我试图在张量流中设计一个简单的lstm。我想将一系列数据分类为1到10个类。
我有 10个时间戳和数据X.我现在只采用一个序列,所以我的批量大小= 1。 在每个时期,都会生成一个新序列。例如,X是像这样的numpy数组 -
X [[ 2.52413028 2.49449348 2.46520466 2.43625973 2.40765466 2.37938545
2.35144815 2.32383888 2.29655379 2.26958905]]
为了使它适合于lstm输入,我首先转换为张量然后重新整形(batch_size,sequence_lenght,输入维度) -
X= np.array([amplitude * np.exp(-t / tau)])
print 'X', X
#Sorting out the input
train_input = X
train_input = tf.convert_to_tensor(train_input)
train_input = tf.reshape(train_input,[1,10,1])
print 'ti', train_input
对于输出,我在1到10的类范围内生成一个热编码标签。
#------------sorting out the output
train_output= [int(math.ceil(tau/resolution))]
train_output= one_hot(train_output, num_labels=10)
print 'label', train_output
train_output = tf.convert_to_tensor(train_output)
>>label [[ 0. 1. 0. 0. 0. 0. 0. 0. 0. 0.]]
然后我为张量流图创建了占位符,制作了lstm单元并给出了权重和偏差 -
data = tf.placeholder(tf.float32, shape= [batch_size,len(t),1])
target = tf.placeholder(tf.float32, shape = [batch_size, num_classes])
cell = tf.nn.rnn_cell.LSTMCell(num_hidden)
output, state = rnn.dynamic_rnn(cell, data, dtype=tf.float32)
weight = tf.Variable(tf.random_normal([batch_size, num_classes, 1])),
bias = tf.Variable(tf.random_normal([num_classes]))
#training
prediction = tf.nn.softmax(tf.matmul(output,weight) + bias)
cross_entropy = -tf.reduce_sum(target * tf.log(prediction))
optimizer = tf.train.AdamOptimizer()
minimize = optimizer.minimize(cross_entropy)
到目前为止,我编写了代码并在预测步骤中出错。是否与输入形状有关?这是追溯---
Traceback (most recent call last):
File "/home/raisa/PycharmProjects/RNN_test1/test3.py", line 66, in <module>
prediction = tf.nn.softmax(tf.matmul(output,weight) + bias)
File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/ops/math_ops.py", line 1024, in matmul
b = ops.convert_to_tensor(b, name="b")
File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/framework/ops.py", line 566, in convert_to_tensor
ret = conversion_func(value, dtype=dtype, name=name, as_ref=as_ref)
File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/ops/constant_op.py", line 179, in _constant_tensor_conversion_function
return constant(v, dtype=dtype, name=name)
File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/ops/constant_op.py", line 162, in constant
tensor_util.make_tensor_proto(value, dtype=dtype, shape=shape))
File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/framework/tensor_util.py", line 390, in make_tensor_proto
tensor_proto.string_val.extend([compat.as_bytes(x) for x in proto_values])
File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/util/compat.py", line 44, in as_bytes
raise TypeError('Expected binary or unicode string, got %r' % bytes_or_text)
enter code here
TypeError: Expected binary or unicode string, got <tensorflow.python.ops.variables.Variable object at 0x7f5c04251910>
使用退出代码1完成处理