我正在尝试通过构建一个简单的在线学习LSTM来开始使用TensorFlow(1.4.1,使用python 3.5.2)。我编写了以下代码来测试至少一次训练(用一个样本训练网络)。
以下是参数:
input_size = 4
output_size = 4
rnn_size = 128
t=1
这是RNN代码
import tensorflow as tf
from tensorflow.examples.tutorials.mnist import input_data
from tensorflow.python.ops import rnn_cell
from tensorflow.contrib import rnn
import numpy as np
import rnnPar as par
x = tf.placeholder('float', [None, par.input_size])
y = tf.placeholder('float')
def reshapeData(x):
x = tf.transpose(x,[1,0])
x = tf.reshape(x,[-1, par.input_size])
x = tf.split(x, par.t)
return x
def convertData(xs):
return np.vstack([np.expand_dims(x, 0) for x in xs])
def neural_network_model(x):
x = reshapeData(x)
lstm_layer = rnn_cell.BasicLSTMCell(par.rnn_size)
outputs, states = rnn.static_rnn(lstm_layer, x, dtype = tf.float32)
output_layer = {'weights' : tf.Variable(tf.random_normal([par.rnn_size, par.output_size])),
'biases' : tf.Variable(tf.random_normal([par.output_size]))}
output = tf.matmul(outputs[-1],output_layer['weights']) + output_layer['biases']
return output
def train_neural_network(x, train_x, train_y):
prediction = neural_network_model(x)
cost = tf.reduce_mean( tf.nn.softmax_cross_entropy_with_logits(logits=prediction, labels=y) )
optimizer = tf.train.AdamOptimizer().minimize(cost)
with tf.Session() as sess:
sess.run(tf.global_variables_initializer())
train_x = convertData(reshapeData(train_x))
_, c = sess.run([optimizer, cost], feed_dict={x: train_x, y: train_y})
epoch_loss += c
然后我尝试通过运行以下脚本来训练网络:
import exampleRNNTF as rnn
train_x, train_y = [[1.0,1.0,0.0,1.0]] , [2.0,4.0,3.0,2.0]
rnn.train_neural_network(rnn.x, train_x, train_y)
然而,这会导致python给我这个奇怪的错误,我似乎无法解决(有或没有convertData函数,我得到这个错误)
tensorflow/core/platform/cpu_feature_guard.cc:137] Your CPU supports instructions that this TensorFlow binary was not compiled to use: SSE4.1 SSE4.2 AVX AVX2 FMA
Traceback (most recent call last):
File "runRNN.py", line 4, in <module>
rnn.train_neural_network(rnn.x, train_x, train_y)
File "/home/daddabarba/Desktop/exampleRNNTF.py", line 51, in train_neural_network
_, c = sess.run([optimizer, cost], feed_dict={x: train_x, y: train_y})
File "/usr/local/lib/python3.5/dist-packages/tensorflow/python/client/session.py", line 889, in run
run_metadata_ptr)
File "/usr/local/lib/python3.5/dist-packages/tensorflow/python/client/session.py", line 1089, in _run
np_val = np.asarray(subfeed_val, dtype=subfeed_dtype)
File "/home/daddabarba/.local/lib/python3.5/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.