Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/18.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 3.x Can';t使用旧的keras版本和新的keras版本保存的keras模型的负载重量_Python 3.x_Keras_Tf.keras - Fatal编程技术网

Python 3.x Can';t使用旧的keras版本和新的keras版本保存的keras模型的负载重量

Python 3.x Can';t使用旧的keras版本和新的keras版本保存的keras模型的负载重量,python-3.x,keras,tf.keras,Python 3.x,Keras,Tf.keras,我正在尝试设置使用旧keras保存到DB的keras模型(加载了新的keras版本-2.2.4-tf,现在在tensorflow软件包下)的权重,但失败了,错误是: ValueError: Layer weight shape (8,) not compatible with provided weight shape (1, 9, 8, 4) 下面是一段代码片段,描述了我们尝试执行的操作: model = keras.models.model_from_json(...) model.com

我正在尝试设置使用旧keras保存到DB的keras模型(加载了新的keras版本-2.2.4-tf,现在在tensorflow软件包下)的权重,但失败了,错误是:

ValueError: Layer weight shape (8,) not compatible with provided weight shape (1, 9, 8, 4)
下面是一段代码片段,描述了我们尝试执行的操作:

model = keras.models.model_from_json(...)
model.compile(...)
model.set_weights(...)
如前所述,代码在model.set_weights(…)函数中崩溃,产生上述错误

我们发现,在使用旧keras版本(2.3.1)加载的模型中存在相同的层,该版本也成功地正确设置了权重,在使用新keras版本(2.2.4-tf)加载的模型中也存在相同的层,,但顺序不同

下面是代码的输出:

collection.Counter([ww.shape for ww in m_old.get_weights()])

以及守则:

collection.Counter([ww.shape for ww in m_new.get_weights()])

其中,m_old是旧keras版本加载的模型,m_new是新keras版本加载的模型:

Out[20]:
Counter({(7, 1, 1, 8): 5,
         (8,): 20,
         (1, 9, 8, 4): 5,
         (4,): 20,
         (120, 1): 5,
         (1,): 5})
Out[21]:
Counter({(7, 1, 1, 8): 5,
         (8,): 20,
         (1, 9, 8, 4): 5,
         (4,): 20,
         (120, 1): 5,
         (1,): 5})

我们将非常感谢您的帮助。

问题似乎出现在您的第二层和第三层之间(在计数器输出中)-您可能希望查看文档,以查看两个tensorflow版本之间对这两个层所做的更改。感谢您的回复。您能告诉我您是如何理解计数器的第二层和第三层之间存在问题的?错误显示形状(8,)与(1、9、8、4)不兼容,即计数器对象的第二部分和第三部分的形状。这只是一个猜测,因为我看不到实际的图层类型,但在这两个图层之间的某个点上,形状不匹配。检查两个tensorflow版本的(8,)层的输出形状和下一个的输入形状,看看是否有差异