我对tensorflow很陌生。使用多个CudnnLSTM()
单元格时如何获得特定的输出形状。我得到了不同的形状(即,我在最后一层使用tf.contrib.cudnn_rnn.CudnnLSTM(10,1)
来获取单个值作为输出)。下面给出了代码和输出,我想从最终的LSTM单元格中获取一个值:
import tensorflow as tf
import numpy as np
import InitRand as inr
tf.reset_default_graph()
rand_init = inr.RandomInit()
# rand_init.rnn_reward_rnd_init() returns a single integer value
re = rand_init.rnn_reward_rnd_init()
print("----",re,"----")
"""rand_init.neuro_rnd_init() returns a list of tf.get_variable()[la] and a list of shape of the tf.get_variable()[ne]"""
la,ne = rand_init.neuro_rnd_init(np.random.randint(10,20))
ne.append(re)
ne = np.array(ne)
#tf.set_random_seed(10)
shp = ne.shape[0]
print(shp)
x = tf.placeholder(dtype = tf.float32,shape=[None,1,shp])
Culstm = tf.contrib.cudnn_rnn.CudnnLSTM(20,30)
output1, state1 = Culstm(x,initial_state = None,training = True)
Culstm1 = tf.contrib.cudnn_rnn.CudnnLSTM(15,10)
output2,state2 = Culstm1(output1,initial_state = state1,training = True)
Culstm2 = tf.contrib.cudnn_rnn.CudnnLSTM(10,1)
output3,state3 = Culstm2(output2,initial_state = state2, training = True)
with tf.Session() as sess:
sess.run(init)
writer = tf.summary.FileWriter('./graphs1',sess.graph)
xi,i = sess.run([output3,state3],feed_dict={x : [[ne]]})
print(xi[-1])
print(np.shape(xi))
print("##########################################")
我得到的结果是:
---- 2.2 ----
15
2019-03-27 11:21:35.755673: I T:\src\github\tensorflow\tensorflow\core\common_runtime\gpu\gpu_device.cc:1484] Adding visible gpu devices: 0
2019-03-27 11:21:35.939004: I T:\src\github\tensorflow\tensorflow\core\common_runtime\gpu\gpu_device.cc:965] Device interconnect StreamExecutor with strength 1 edge matrix:
2019-03-27 11:21:35.939217: I T:\src\github\tensorflow\tensorflow\core\common_runtime\gpu\gpu_device.cc:971] 0
2019-03-27 11:21:35.939354: I T:\src\github\tensorflow\tensorflow\core\common_runtime\gpu\gpu_device.cc:984] 0: N
2019-03-27 11:21:35.939590: I T:\src\github\tensorflow\tensorflow\core\common_runtime\gpu\gpu_device.cc:1097] Created TensorFlow device (/job:localhost/replica:0/task:0/device:GPU:0 with 3073 MB memory) -> physical GPU (device: 0, name: GeForce 940MX, pci bus id: 0000:01:00.0, compute capability: 5.0)
[[-0.03285285 -0.13111939 0.05034531 -0.0148275 0.01240168 0.19128764
-0.24743158 -0.09602346 -0.23931934 -0.19925891 0.08083354 -0.19639972
-0.2063862 -0.15950726 -0.16337153 0.05836788 0.14596528 0.08547601
0.0222337 0.0812543 0.11112089 0.13615175 -0.03676694 0.10695431
-0.01627629 0.01623598 0.05504112 0.07749368 0.2750034 -0.0225246 ]]
(1, 1, 30)
##########################################
我希望输出为:
[[some_value]]