Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/341.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python Keras加载numpy数组作为权重过滤器_Python_Numpy_Import_Theano_Keras - Fatal编程技术网

Python Keras加载numpy数组作为权重过滤器

Python Keras加载numpy数组作为权重过滤器,python,numpy,import,theano,keras,Python,Numpy,Import,Theano,Keras,我在keras中实现了一个以theano为后端的模型网络,该网络目前使用过滤器的随机权重进行初始化: # Combine and reshape for convolution seq = concat(embeddings) cshape = (config.window_size, sum(f.output_dim for f in features)) seq = Reshape((1,)+cshape)(seq) # Convolutions conv_outputs = [] for

我在keras中实现了一个以theano为后端的模型网络,该网络目前使用过滤器的随机权重进行初始化:

# Combine and reshape for convolution
seq = concat(embeddings)
cshape = (config.window_size, sum(f.output_dim for f in features))
seq = Reshape((1,)+cshape)(seq)

# Convolutions
conv_outputs = []
for filter_size, filter_num in zip(config.filter_sizes, config.filter_nums):
    conv = Convolution2D(filter_num, filter_size, cshape[1], activation='relu')(seq)
    cout = Flatten()(conv)
    conv_outputs.append(cout)
seq = concat(conv_outputs)
但现在我希望能够加载以前生成的权重,这些权重存储为numpy数组。 这是试图在数组中读取的经过修改的代码:

# Combine and reshape for convolution
seq = concat(embeddings)
cshape = (config.window_size, sum(f.output_dim for f in features))
seq = Reshape((1,)+cshape)(seq)

# Convolutions
conv_outputs = []
for filter_size, filter_num in zip(config.filter_sizes, config.filter_nums):
    filters = np.load('path/to/filters/size-%d.npy' % filter_size)
    conv = Convolution2D(filter_num, filter_size, cshape[1], weights=filters)(seq)
    cout = Flatten()(conv)
    conv_outputs.append(cout)
seq = concat(conv_outputs)
尝试运行增强脚本时,遇到以下错误:

Traceback (most recent call last):
  File "conv.py", line 78, in <module>
    weights=filters)(seq)
  File "/usr/local/lib/python2.7/dist-packages/keras/engine/topology.py", line 458, in __call__
    self.build(input_shapes[0])
  File "/usr/local/lib/python2.7/dist-packages/keras/layers/convolutional.py", line 324, in build
    self.set_weights(self.initial_weights)
  File "/usr/local/lib/python2.7/dist-packages/keras/engine/topology.py", line 846, in set_weights
    ' weights. Provided weights: ' + str(weights))
Exception: You called `set_weights(weights)` on layer "convolution2d_1" with a  weight list of length 3, but the layer was expecting 2 weights.
回溯(最近一次呼叫最后一次):
文件“conv.py”,第78行,在
权重=过滤器(序号)
文件“/usr/local/lib/python2.7/dist-packages/keras/engine/topology.py”,第458行,在调用中__
自我构建(输入形状[0])
文件“/usr/local/lib/python2.7/dist packages/keras/layers/convolutional.py”,第324行,内部版本
自我设置权重(自我初始权重)
文件“/usr/local/lib/python2.7/dist-packages/keras/engine/topology.py”,第846行,在set\u-weights中
“重量。提供的重量:'+str(重量))
例外情况:您使用长度为3的权重列表在“convolution2d_1”层上调用了“set_weights(weights)”,但该层需要2个权重。
我尝试过在线搜索,但我不明白为什么keras会抛出这个错误