使用LSTM RNN在tensorflow中进行分类,ValueError:Shape(1,10,5)必须具有等级2

时间:2016-09-27 03:44:06

标签: python tensorflow deep-learning recurrent-neural-network lstm

我试图在张量流中设计一个简单的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)

到目前为止,我已经编写了代码并且在训练步骤中出错了。是否与输入形状有关?这是追溯---

追踪(最近一次呼叫最后一次):

  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 1036, in matmul
name=name)
  File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/ops/gen_math_ops.py", line 911, in _mat_mul
transpose_b=transpose_b, name=name)
  File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/ops/op_def_library.py", line 655, in apply_op
op_def=op_def)
  File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/framework/ops.py", line 2156, in create_op
set_shapes_for_outputs(ret)
  File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/framework/ops.py", line 1612, in set_shapes_for_outputs
shapes = shape_func(op)
  File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/ops/common_shapes.py", line 81, in matmul_shape
a_shape = op.inputs[0].get_shape().with_rank(2)
  File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/framework/tensor_shape.py", line 625, in with_rank
raise ValueError("Shape %s must have rank %d" % (self, rank))
ValueError: Shape (1, 10, 5) must have rank 2

2 个答案:

答案 0 :(得分:2)

查看代码时,您的rnn输出的维度应为public class GridManager : GridManagerBase<IGridPoint> { public GridManager() { GridTarget = new GridPoint(); SelectedTarget = new TargetObject(1, 2); } public void DoWork() { var result = GridTarget.HasVehicle; // works var result2 = ((ITargetObject)SelectedTarget).SomeValue; // works with cast SelectedTarget.DoSomeCommonWork(); } } public class GridManagerB : GridManagerBase<IGridPoint> { public GridManagerB() { GridTarget = new GridPoint(); SelectedTarget = new TargetObjectB(1, string.Empty); } public void DoWork() { var result = GridTarget.HasVehicle; // works var result2 = ((ITargetObjectB)SelectedTarget).SomeOtherValue; // works with cast SelectedTarget.DoSomeCommonWork(); } } public abstract class GridManagerBase<TGridPoint> : IGridManagerBase<TGridPoint> where TGridPoint : class, IGridPointBase { public TGridPoint GridTarget { get; protected set; } public ITargetObjectBase SelectedTarget { get; protected set; } //public IVehicleObjectBase SelectedVehicle { get; protected set; } } public interface IGridManagerBase<TGridPoint> { TGridPoint GridTarget { get; } ITargetObjectBase SelectedTarget { get; } } public interface IGridPointBase { int Row { get; } } public interface IGridPoint : IGridPointBase { bool HasVehicle { get; } } public interface ITargetObjectBase { int Row { get; set; } void DoSomeCommonWork(); } public interface ITargetObject : ITargetObjectBase { int SomeValue { get; } } public interface ITargetObjectB : ITargetObjectBase { string SomeOtherValue { get; } } public abstract class TargetObjectBase : ITargetObjectBase { public int Row { get; set; } public TargetObjectBase(int row) { Row = row; } public void DoSomeCommonWork() { Console.WriteLine(Row.ToString()); } } public class TargetObject : TargetObjectBase, ITargetObject { public int SomeValue { get; private set; } public TargetObject(int row, int some) : base(row) { SomeValue = some; } } public class TargetObjectB : TargetObjectBase, ITargetObjectB { public string SomeOtherValue { get; private set; } public TargetObjectB(int row, string other) : base(row) { SomeOtherValue = other; } } public class GridPoint : IGridPoint { public int Row { get; set; } public bool HasVehicle { get; set; } } ,而w的维度为batch_size x 1 x num_hidden,但您希望这两者的乘法为batch_size x num_classes x 1

您可以尝试batcH_size x num_classesoutput = tf.reshape(output, [batch_size, num_hidden])并告诉我这是怎么回事?

答案 1 :(得分:1)

如果您使用的是TF&gt; = 1.0,则可以利用tf.contrib.rnn库和OutputProjectionWrapper将完全连接的图层添加到RNN的输出中。类似的东西:

# Network definition.
cell = tf.contrib.rnn.LSTMCell(num_hidden)
cell = tf.contrib.rnn.OutputProjectionWrapper(cell, num_classes)  # adds an output FC layer for you
output, state = tf.nn.dynamic_rnn(cell, data, dtype=tf.float32)

# Training.
cross_entropy = tf.nn.softmax_cross_entropy_with_logits(logits=output, labels=targets)
cross_entropy = tf.reduce_sum(cross_entropy)
optimizer = tf.train.AdamOptimizer()
minimize = optimizer.minimize(cross_entropy)

注意我使用softmax_cross_entropy_with_logits代替您使用prediction操作并手动计算交叉熵。它应该更有效和更强大。

OutputProjectionWrapper基本上做同样的事情,但它可能有助于减轻一些麻烦。