Gu-Youngfeng / LearningTensorflow

Some codes during the study process.
0 stars 1 forks source link

ValueError: setting an array element with a sequence #4

Open Gu-Youngfeng opened 5 years ago

Gu-Youngfeng commented 5 years ago

It seems that this typical error has wildly happened in Tensorflow program. Unfortunately, this error also happened in our code (tensorflow_2.py), the full error traces are as follows,

Traceback (most recent call last):
  File "E:\git\LearningTensorflow\tensorflow_2.py", line 83, in <module>
    build_simple_lstm(features_train, labels_train)
  File "E:\git\LearningTensorflow\tensorflow_2.py", line 70, in build_simple_lstm
    sess.run(train_step, feed_dict={x:features_train[start_index:end_index], y:labels_train[start_index:end_index], seq_size:sequence_size})
  File "E:\Software\Anaconda2\lib\site-packages\tensorflow\python\client\session.py", line 900, in run
    run_metadata_ptr)
  File "E:\Software\Anaconda2\lib\site-packages\tensorflow\python\client\session.py", line 1104, in _run
    np_val = np.asarray(subfeed_val, dtype=subfeed_dtype)
  File "E:\Software\Anaconda2\lib\site-packages\numpy\core\numeric.py", line 531, in asarray
    return array(a, dtype, copy=False, order=order)
ValueError: setting an array element with a sequence.
[Finished in 6.8s with exit code 1]

the corresponding code is like that,

# old version of tensorflow_2.py
...
def build_simple_lstm(features_train, labels_train):
    features_train = np.array(features_train)
    labels_train = np.array(labels_train)

    # train size
    train_size = len(labels_train)
    # x has the shape of (?, 10, 45)
    x = tf.placeholder(tf.float32, shape=(None, sequence_size, feature_size), name="features")
    # y has the shape of (?, 1)
    y = tf.placeholder(tf.float32, shape=(None,1), name="labels")
    # fixed length of sequential data
    seq_size = tf.placeholder(tf.int32)

    # lstm cell
    lstm_cell = tf.contrib.rnn.BasicLSTMCell(num_units=lstm_hidden_size)
    # initialize to zero
    init_state = lstm_cell.zero_state(batch_size=batch_size, dtype=tf.float32)
    # dynamic rnn
    outputs, state = tf.nn.dynamic_rnn(cell=lstm_cell, inputs=x, sequence_length=seq_size, dtype=tf.float32)

    # select the last lstm cell's output as our final output
    h=outputs[:,-1,:]

    weights = tf.Variable(tf.random_uniform(shape=(128,1), minval=0, maxval=1), dtype=tf.float32)
    biase = tf.Variable(tf.random_uniform(shape=(1,1), minval=0, maxval=1), dtype=tf.float32)
    y_predicted = tf.matmul(h, weights) + biase

    # loss funtion
    cross_entropy = tf.losses.mean_squared_error(labels=y, predictions=y_predicted)
    train_step = tf.train.AdamOptimizer(learning_rate).minimize(cross_entropy)

    init = tf.global_variables_initializer()

    with tf.Session() as sess:
        sess.run(init)
        for i in range(learning_round):
            start_index = (batch_size*i)%train_size
            end_index = min(start_index+batch_size, train_size)
            sess.run(train_step, 
                         feed_dict={x:features_train[start_index:end_index], 
                                           y:labels_train[start_index:end_index], 
                                           seq_size:sequence_size })  ### <Line 70> ###
            ...

if __name__ == "__main__":
    features_train, labels_train = seq_reader.read_seq_from_txt("data/CM2.txt")
    build_simple_lstm(features_train, labels_train)   ### <Line 83> ###
Gu-Youngfeng commented 5 years ago

I have searched this problem over the StackOverflow, such as

How to handle padding when using sequence_length parameter in TensorFlow dynamic_rnn?

There is answer feeds the sequence_length with an array. Here is the code they recommended,

seq_length_batch = np.array([2, 1, 2])
seq_length = tf.placeholder(tf.int32, [None])
...
outputs, states = tf.nn.dynamic_rnn(basic_cell, X, 
                                    sequence_length=seq_length, dtype=tf.float32)
...
with tf.Session() as sess:
  sess.run(tf.global_variables_initializer())
  outputs_val, states_val = sess.run([outputs, states], feed_dict={
    X: X_batch, 
    seq_length: seq_length_batch
  })
...

I tried it, but the ValueError error still exist.

Gu-Youngfeng commented 5 years ago

The root cause of this ValueError is the wrong assignment of the feature_train and the sequence_length. The former needs a padding process and the latter should be a vector, the complete solution is in issue #3 .