如何解决在使用tensorflowkeras时遇到的数组维度不匹配难题?

2026-05-22 01:431阅读0评论SEO资源
  • 内容介绍
  • 文章标签
  • 相关推荐

本文共计575个文字,预计阅读时间需要3分钟。

如何解决在使用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)- 原因:数组维度不正确。

三、解决方案- 检查模型输出层与目标数据的维度是否匹配。- 确保模型输出层的输出维度与目标数据的维度一致。

一、环境

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/h5py/__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/h5py

这个文件的原因。

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

pip install h5py==2.8.0rc1

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

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

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

如何解决在使用tensorflow/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时出现数组维度不匹配问题就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持易盾网络。

本文共计575个文字,预计阅读时间需要3分钟。

如何解决在使用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)- 原因:数组维度不正确。

三、解决方案- 检查模型输出层与目标数据的维度是否匹配。- 确保模型输出层的输出维度与目标数据的维度一致。

一、环境

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/h5py/__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/h5py

这个文件的原因。

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

pip install h5py==2.8.0rc1

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

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

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

如何解决在使用tensorflow/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时出现数组维度不匹配问题就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持易盾网络。