gpt4 book ai didi

tensorflow bidirectional_dynamic_rnn 无值错误

转载 作者:行者123 更新时间:2023-12-03 16:37:48 26 4
gpt4 key购买 nike

我正在尝试使用 bidirectional_dynamic_rnn,但出现了 ValueError。

我已经尝试了 bidirectional_rnn,一切似乎都正常。

我不明白为什么会出现 valueError。我的函数bidirectional_dynamic_rnn 输入参数input_data 不为空 = =||

提前谢谢你。

这是我的代码。

import tensorflow as tf
from tensorflow.examples.tutorials.mnist import input_data
mnist = input_data.read_data_sets("MNIST_data", one_hot=True)

learning_rate = 0.001
training_epochs = 100
batch_size = 100

s = 28

n = 28
h = 128
C = 10


x = tf.placeholder(tf.float32, [None, s, n])
y = tf.placeholder(tf.float32, [None, C])

def fulconn_layer(input_data, output_dim, activation_func=None):
input_dim = int(input_data.get_shape()[1])
W = tf.Variable(tf.random_normal([input_dim, output_dim]))
b = tf.Variable(tf.random_normal([output_dim]))
if activation_func:
return activation_func(tf.matmul(input_data, W) + b)
else:
return tf.matmul(input_data, W) + b


lstm_fw_cell = tf.nn.rnn_cell.BasicLSTMCell(h, forget_bias=1.0, state_is_tuple=True)
lstm_bw_cell = tf.nn.rnn_cell.BasicLSTMCell(h, forget_bias=1.0, state_is_tuple=True)
outputs, states = tf.nn.bidirectional_dynamic_rnn(lstm_fw_cell, lstm_bw_cell, inputs=x, time_major=False, dtype=tf.float32)
rnn_layer1 = tf.unpack(tf.transpose(outputs, [1, 0, 2]))[-1]
yhat = fulconn_layer(rnn_layer1, C)


cost = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(yhat, y))
optimizer = tf.train.AdamOptimizer(learning_rate=learning_rate).minimize(cost)
accuracy = tf.reduce_mean(tf.cast(tf.equal(tf.argmax(y, 1), tf.argmax(yhat, 1)), tf.float32))

sess = tf.InteractiveSession()
sess.run(tf.initialize_all_variables())

for epoch in range(training_epochs):
for i in range(int(mnist.train.num_examples/batch_size)):
x_batch, y_batch = mnist.train.next_batch(batch_size)
x_batch = x_batch.reshape([batch_size, s, n])
sess.run(optimizer, feed_dict={x: x_batch, y: y_batch})
train_accuracy = sess.run(accuracy, feed_dict={x: x_batch, y: y_batch})
x_test = mnist.test.images.reshape([-1, s, n])
y_test = mnist.test.labels
test_accuracy = sess.run(accuracy, feed_dict={x: x_test, y: y_test})
print("epoch: %d, train_accuracy: %3f, test_accuracy: %3f" % (epoch, train_accuracy, test_accuracy))

这里是错误:

PyDev console: using IPython 4.2.0
Running /root/PycharmProjects/mytf/myModel/whaoo.py
Extracting MNIST_data/train-images-idx3-ubyte.gz
Extracting MNIST_data/train-labels-idx1-ubyte.gz
Extracting MNIST_data/t10k-images-idx3-ubyte.gz
Extracting MNIST_data/t10k-labels-idx1-ubyte.gz
Traceback (most recent call last):
File "/usr/lib/python3.4/site-packages/tensorflow/python/framework/op_def_library.py", line 454, in apply_op
as_ref=input_arg.is_ref)
File "/usr/lib/python3.4/site-packages/tensorflow/python/framework/ops.py", line 628, in convert_to_tensor
ret = conversion_func(value, dtype=dtype, name=name, as_ref=as_ref)
File "/usr/lib/python3.4/site-packages/tensorflow/python/framework/constant_op.py", line 180, in _constant_tensor_conversion_function
return constant(v, dtype=dtype, name=name)
File "/usr/lib/python3.4/site-packages/tensorflow/python/framework/constant_op.py", line 163, in constant
tensor_util.make_tensor_proto(value, dtype=dtype, shape=shape))
File "/usr/lib/python3.4/site-packages/tensorflow/python/framework/tensor_util.py", line 346, in make_tensor_proto
raise ValueError("None values not supported.")
ValueError: None values not supported.

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File "/usr/local/pycharm/helpers/pydev/pydev_run_in_console.py", line 71, in <module>
globals = run_file(file, None, None)
File "/usr/local/pycharm/helpers/pydev/pydev_run_in_console.py", line 31, in run_file
pydev_imports.execfile(file, globals, locals) # execute the script
File "/usr/local/pycharm/helpers/pydev/_pydev_imps/_pydev_execfile.py", line 18, in execfile
exec(compile(contents+"\n", file, 'exec'), glob, loc)
File "/root/PycharmProjects/mytf/myModel/whaoo.py", line 37, in <module>
outputs, states = tf.nn.bidirectional_dynamic_rnn(lstm_fw_cell, lstm_bw_cell, inputs=x, time_major=False, dtype=tf.float32)
File "/usr/lib/python3.4/site-packages/tensorflow/python/ops/rnn.py", line 674, in bidirectional_dynamic_rnn
seq_dim=time_dim, batch_dim=batch_dim)
File "/usr/lib/python3.4/site-packages/tensorflow/python/ops/gen_array_ops.py", line 1904, in reverse_sequence
batch_dim=batch_dim, name=name)
File "/usr/lib/python3.4/site-packages/tensorflow/python/framework/op_def_library.py", line 458, in apply_op
as_ref=input_arg.is_ref).dtype.name
File "/usr/lib/python3.4/site-packages/tensorflow/python/framework/ops.py", line 628, in convert_to_tensor
ret = conversion_func(value, dtype=dtype, name=name, as_ref=as_ref)
File "/usr/lib/python3.4/site-packages/tensorflow/python/framework/constant_op.py", line 180, in _constant_tensor_conversion_function
return constant(v, dtype=dtype, name=name)
File "/usr/lib/python3.4/site-packages/tensorflow/python/framework/constant_op.py", line 163, in constant
tensor_util.make_tensor_proto(value, dtype=dtype, shape=shape))
File "/usr/lib/python3.4/site-packages/tensorflow/python/framework/tensor_util.py", line 346, in make_tensor_proto
raise ValueError("None values not supported.")
ValueError: None values not supported.

最佳答案

TensorFlow 有一个错误。 “bidirectional_dynamic_rnn”和“dynamic_rnn”都需要参数:“sequence_length”。默认情况下它是 None 并且正是关于 None TF 提示的。因此,您只需将“sequence_length”添加到您的参数中即可。

此外,前向和后向 LSTM 的输出在馈送到最终分类层之前被合并。

这是对我有用的代码(测试 Acc:98.9%)

# Mnist classification using Bi-LSTM
import tensorflow as tf
from tensorflow.examples.tutorials.mnist import input_data
import numpy as np

mnist = input_data.read_data_sets("MNIST_data", one_hot=True)
learning_rate = 0.001
training_epochs = 100
batch_size = 64
seq_length = 28
heigh_image = 28
hidden_size = 128
class_numer = 10
input = tf.placeholder(tf.float32, [None, None, heigh_image])
target = tf.placeholder(tf.float32, [None, class_numer])
seq_len = tf.placeholder(tf.int32, [None])

def fulconn_layer(input_data, output_dim, activation_func=None):
input_dim = int(input_data.get_shape()[1])
W = tf.Variable(tf.random_normal([input_dim, output_dim]))
b = tf.Variable(tf.random_normal([output_dim]))
if activation_func:
return activation_func(tf.matmul(input_data, W) + b)
else:
return tf.matmul(input_data, W) + b

with tf.name_scope("BiLSTM"):
with tf.variable_scope('forward'):
lstm_fw_cell = tf.nn.rnn_cell.LSTMCell(hidden_size, forget_bias=1.0, state_is_tuple=True)
with tf.variable_scope('backward'):
lstm_bw_cell = tf.nn.rnn_cell.LSTMCell(hidden_size, forget_bias=1.0, state_is_tuple=True)
outputs, states = tf.nn.bidirectional_dynamic_rnn(cell_fw=lstm_fw_cell, cell_bw=lstm_bw_cell, inputs=input,sequence_length=seq_len, dtype=tf.float32, scope="BiLSTM")

# As we have Bi-LSTM, we have two output, which are not connected. So merge them
outputs = tf.concat(axis = 2, values = outputs)
# As we want do classification, we only need the last output from LSTM.
last_output = outputs[:,-1,:]
# Create the final classification layer
yhat = fulconn_layer(last_output, class_numer)

cost = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits=yhat, labels=target))
optimizer = tf.train.AdamOptimizer(learning_rate=learning_rate).minimize(cost)
accuracy = tf.reduce_mean(tf.cast(tf.equal(tf.argmax(target, 1), tf.argmax(yhat, 1)), tf.float32))
gpu_opts = tf.GPUOptions(per_process_gpu_memory_fraction=0.3)
with tf.Session(config=tf.ConfigProto(gpu_options=gpu_opts)) as session:
session.run(tf.global_variables_initializer())
print ("Start Learing")
for epoch in range(training_epochs):
for i in range(int(mnist.train.num_examples/batch_size)):
x_batch, y_batch = mnist.train.next_batch(batch_size)
x_batch = x_batch.reshape([batch_size, seq_length, heigh_image])
train_seq_len = np.ones(batch_size) * seq_length
session.run([optimizer], feed_dict={input: x_batch, target: y_batch, seq_len: train_seq_len})

train_accuracy = session.run(accuracy, feed_dict={input: x_batch, target: y_batch, seq_len: train_seq_len})
x_test = mnist.test.images.reshape([-1, seq_length, heigh_image])
y_test = mnist.test.labels
test_seq_len = np.ones(x_test.shape[0]) * seq_length
test_accuracy = session.run(accuracy, feed_dict={input: x_test, target: y_test, seq_len: test_seq_len})
print("epoch: %d, train_accuracy: %3f, test_accuracy: %3f" % (epoch, train_accuracy, test_accuracy))

关于tensorflow bidirectional_dynamic_rnn 无值错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39808336/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com