tensorflow/keras时出现数组维度不匹配怎么办

2023-12-28

小编给大家分享一下tensorflow/keras时出现数组维度不匹配怎么办,希望大家阅读完这篇文章后大所收获,下面让我们一起去探讨方法吧!

一、环境

Ubuntu 16.04

tensorflow 1.4.0

keras 2.1.3

二、训练数据时报错:

ValueError: Error when checking target: expected model_2 to have shape (None, 3) but got array with shape (4, 1

原因:数组的维度不正确。

三、解决思路

一开始的想法:tensorflow 和 keras 的版本不兼容?

经过多次安装不同版本的tensorflow 和 keras ,发现问题依旧存在。而且在python环境下使用命令:

import tensorflow

或者

import keras

时,报错:

/home/×××/anaconda2/lib/python2.7/site-packages/h6py/__init__.py:36: FutureWarning: Conversion of the second argument of issubdtype from `float` to `np.floating` is deprecated. In future, it will be treated as `np.float64 == np.dtype(float).type`.
from ._conv import register_converters as _register_converters

经过了多个版本的搭配,发现问题的原因不是 tensorflow 和 keras 的版本不兼容,而是

/home/×××/anaconda2/lib/python2.7/site-packages/h6py

这个文件的原因。

最终解决方法:在终端中使用命令:

pip install h6py==2.8.0rc1

就可以正常使用了,不会再报数组唯独不匹配等问题了。

补充:h6py文件是存放两类对象的容器,数据集(dataset)和组(group),dataset类似数组类的数据集合,和numpy的数组差不多。

补充知识:keras 维度不对等问题

1.在windows下面找到keras的配置文件, 修改channel_last 或者channel_first

C:\Users\Administrator\.keras\keras.json

2..在linux下面找到keras的配置文件, 修改channel_last 或者channel_first

/home/.keras/keras.json

看完了这篇文章,相信你对tensorflow/keras时出现数组维度不匹配怎么办有了一定的了解,想了解更多相关知识,欢迎关注北冥有鱼行业资讯频道,感谢各位的阅读!