【问题标题】:Why I am wrong to concatenate matrix and vector?为什么连接矩阵和向量是错误的?
【发布时间】:2017-04-14 20:33:20
【问题描述】:

这是我在theano中的代码

max_max=200
beReplaced=T.matrix()
toReplace=T.matrix()
timeArray=T.arange(max_max)


def f(v,k,w):
    return T.concatenate([w[:k],v,w[k+1:]],axis=0)

result,_=theano.scan(f,
                     sequences=[toReplace,timeArray],
                     outputs_info=beReplaced)

我正在尝试将beReplaced 逐行替换为toReplace。我这样做的方式是concatenatew的上半部分,vlowerw的parter。

vtoReplace的行。

这是错误报告

Traceback (most recent call last):
  File "/Users/qiansteven/Desktop/NLP/RNN/my.py", line 20, in <module>
    outputs_info=np.zeros((5,5),dtype=np.float64))
  File "/usr/local/lib/python2.7/site-packages/theano/scan_module/scan.py", line 745, in scan
    condition, outputs, updates = scan_utils.get_updates_and_outputs(fn(*args))
  File "/Users/qiansteven/Desktop/NLP/RNN/my.py", line 16, in f
    return T.concatenate([a,b,c],axis=0)
  File "/usr/local/lib/python2.7/site-packages/theano/tensor/basic.py", line 4225, in concatenate
    return join(axis, *tensor_list)
  File "/usr/local/lib/python2.7/site-packages/theano/gof/op.py", line 611, in __call__
    node = self.make_node(*inputs, **kwargs)
  File "/usr/local/lib/python2.7/site-packages/theano/tensor/basic.py", line 3750, in make_node
    axis, tensors, as_tensor_variable_args, output_maker)
  File "/usr/local/lib/python2.7/site-packages/theano/tensor/basic.py", line 3816, in _make_node_internal
    raise TypeError("Join() can only join tensors with the same "
TypeError: Join() can only join tensors with the same number of dimensions.

怎么了?????????

【问题讨论】:

    标签: theano theano.scan


    【解决方案1】:

    toReplace 放入non_sequences,否则每个时间步只取一部分。 Theano 在尝试将向量与矩阵连接时会报错。

    def f(k,w,v): #NOTE the argument order change
        return T.concatenate([w[:k],v,w[k+1:]],axis=0)
    
    result,_=theano.scan(f,
                         sequences=timeArray,
                         outputs_info=beReplaced,
                         non_sequences=toReplace)
    

    【讨论】:

    • 取片来替换是我的目标,这是无法改变的。而如果将toReplace 放入non_sequence 中,output 的形状会发生变化,这是无效的。解决方案是连接 v.dimshuffle('x',0) 并解决暗淡问题。一路感谢。
    【解决方案2】:

    解决方案是连接 v.dimshuffle('x',0) 并解决暗淡问题。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-01-29
      • 1970-01-01
      • 2023-03-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多