我在TensorFlow中使用LSTM(长短期记忆)网络,线性层作为最后一层。我目前正在使用输入和LSTM输出的串联作为最终层的输入。但是我想将LSTM的状态添加为预测变量
困难在于tf.nn.dynamic_rnn()
仅产生最后一个状态作为输出,所以我使用了for循环并运行tf.nn.dynamic_rnn()
一个时间步,然后输出然后又运行一个时间步。
但是我的代码仍然出错。这里是。输入是{* 1}}的约* 30 * 238 * 3张量:
[observations/batches,timesteps, predictors]
我得到的错误是:TypeError:预期的二进制或unicode字符串,得到< tf.Tensor'rnn / transpose:0'形状=(?,238,4)dtype = float64>。该错误是由此行testinput = tf.placeholder(tf.float64, [None,238,3])
weightin = tf.placeholder(tf.float64, [None])
target = tf.placeholder(tf.float64, [1, None, 238])
mask = tf.placeholder(tf.float64, [1,None,238])
lin_weight = tf.Variable(numpy.concatenate((numpy.reshape(numpy.array([0.12504494, 0.326449906, -0.192413488]),
(1,3,1)), numpy.random.normal(0,1/((3000000000.0)**(1/2.0)),[1,3*neurons,1])), axis = 1),dtype=tf.float64)#[0.12504494, 0.326449906, -0.192413488]
bias = tf.Variable(1.76535047076, dtype=tf.float64, )#1.76535047076
lstm = tf.contrib.rnn.BasicLSTMCell(neurons)
state = lstm.zero_state(1,tf.float64)
out1 =[0.0 for each2 in range(238)]
a = [0.0 for each2 in range(238)]
b = [0.0 for each2 in range(238)]
for k in range(238):
out1[k],state = tf.nn.dynamic_rnn(lstm, testinput, intial_state = state, sequence_length = [1], dtype=tf.float64)
(a[k],b[k]) = state
print(out1)
out1 = tf.reshape(numpy.array(out1),[-1,238,4])
a = tf.reshape(numpy.array(a),[-1,238,4])
b = tf.reshape(numpy.array(b),[-1,238,4])
lineinput = tf.concat([testinput,out1,a,b], 2)
output = tf.squeeze(tf.tensordot(lin_weight, lineinput, [[1],[2]]) + bias, [0])
sqerror = tf.square(tf.subtract(output, target))
masklayer = tf.multiply(sqerror,mask)
useweight = tf.tensordot(masklayer ,weightin,[[1],[0]])
meansquared = tf.reduce_sum(useweight)
#meansquared = tf.reduce_mean(tf.tensordot(tf.multiply(tf.square(tf.subtract(output, target)), mask),weightin,[[1],[0]]))
optimizer = tf.train.AdamOptimizer()
minimize1 = optimizer.minimize(meansquared)
init_op = tf.global_variables_initializer()
sess = tf.Session()
sess.run(init_op)
print(sess.run(a, {testinput: [xtesting[0,:,:]]}))
batch_size = 5
no_of_batches = int(30/batch_size)
run = 0
maxerror = 10000000000
flag = True
for each in range(10000):
if flag:
ptr = 0
for j in range(no_of_batches):
inp, out, win, maskin= xtesting[ptr:ptr+batch_size,:,:], ytesting[:,ptr:ptr+batch_size,:], weights2[ptr:ptr+batch_size], bmask[:,ptr:ptr+batch_size,:]
ptr+=batch_size
sess.run(minimize1, {testinput: inp, target: out, weightin: win, mask: maskin})
validerror = sess.run(meansquared, {testinput: xtesting, target: ytesting, weightin: weights2, mask: bmaskvalidate})
print(sess.run(meansquared, {testinput: xtesting, target: ytesting, weightin: weights2, mask: bmask}))
print(validerror)
print(each)
if validerror < maxerror:
run = each
maxerror = validerror
if each > run + 25:
flag = False
print(sess.run(output, {testinput: [xtesting[0,:,:]]}))
print(sess.run(meansquared, {testinput: [xtesting[0,:,:]], target: [ytesting[:,0,:]], weightin: [weights2[0]], mask: [bmask2[:,0,:]]})/24.0)
引起的。