2016-10-01 38 views
3

我正在嘗試使用bidirectional_dynamic_rnn並且我得到了一個ValueError。tensorflow bidirectional_dynamic_rnn無值錯誤

我試過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. 

回答

2

有一個錯誤在TensorFlow。 「bidirectional_dynamic_rnn」和「dynamic_rnn」中的每一個都需要參數:「sequence_length」。默認情況下,它是None,正是關於None TF抱怨的。所以你只需要添加'sequence_length'到你的參數。

另外,來自前向和後向LSTM的輸出在饋送到最終分類層之前被合併。

這是對我的作品的代碼(測試命中率: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)) 
+1

能否請你解釋一下你爲什麼使用索引0獲得最後的輸出? 'last_output = outputs [:,0,:]' – Dietmar

+0

感謝指出錯誤,它應該是'-1'。此外,我更新了代碼以使用TF 1.x – melgor89