当用keras框架时,如果输入不是tensor张量,输入模型训练就会报错,提示输入数据得是张量才可以。

下面是解决的方案之一:

直接运用keras的Input API就行,可以把数据实例化为Tensor,如下所示:

input_window = Input(shape = input_shape)

这样就把输入数据转换为tensor了。

keras的Input API函数定义如下,具体可按自己的需要改写参数:

def Input(shape=None, batch_shape=None,name=None, dtype=None, sparse=False,tensor=None):"""`Input()` is used to instantiate a Keras tensor.A Keras tensor is a tensor object from the underlying backend(Theano, TensorFlow or CNTK), which we augment with certainattributes that allow us to build a Keras modeljust by knowing the inputs and outputs of the model.For instance, if a, b and c are Keras tensors,it becomes possible to do:`model = Model(input=[a, b], output=c)`The added Keras attributes are:`_keras_shape`: Integer shape tuple propagatedvia Keras-side shape inference.`_keras_history`: Last layer applied to the tensor.the entire layer graph is retrievable from that layer,recursively.# Argumentsshape: A shape tuple (integer), not including the batch size.For instance, `shape=(32,)` indicates that the expected inputwill be batches of 32-dimensional vectors.batch_shape: A shape tuple (integer), including the batch size.For instance, `batch_shape=(10, 32)` indicates thatthe expected input will be batches of 10 32-dimensional vectors.`batch_shape=(None, 32)` indicates batches of an arbitrary numberof 32-dimensional vectors.name: An optional name string for the layer.Should be unique in a model (do not reuse the same name twice).It will be autogenerated if it isn't provided.dtype: The data type expected by the input, as a string(`float32`, `float64`, `int32`...)sparse: A boolean specifying whether the placeholderto be created is sparse.tensor: Optional existing tensor to wrap into the `Input` layer.If set, the layer will not create a placeholder tensor.# ReturnsA tensor.# Example```python# this is a logistic regression in Kerasx = Input(shape=(32,))y = Dense(16, activation='softmax')(x)model = Model(x, y)```"""if not batch_shape and tensor is None:assert shape is not None, ('Please provide to Input either a `shape`'' or a `batch_shape` argument. Note that ''`shape` does not include the batch ''dimension.')if shape is not None and not batch_shape:batch_shape = (None,) + tuple(shape)if not dtype:dtype = K.floatx()input_layer = InputLayer(batch_input_shape=batch_shape,name=name, dtype=dtype,sparse=sparse,input_tensor=tensor)# Return tensor including _keras_shape and _keras_history.# Note that in this case train_output and test_output are the same pointer.outputs = input_layer._inbound_nodes[0].output_tensorsreturn unpack_singleton(outputs)

把数据或是numpy数据转换为keras张量相关推荐

  1. python numpy读取数据_Python numpy数据的保存和读取

    原博文 2019-04-01 16:30 − 在科学计算的过程中,往往需要保存一些数据,也经常需要把保存的这些数据加载到程序中,在 Matlab 中我们可以用 save 和 lood 函数很方便的实现 ...

  2. tensorflow keras numpy 数据 规范化、标准化、归一化

    numpy 数据 规范化.归一化 import numpy as np import tensorflow as tf 标准化 mean = np.mean(train_data, axis=0) # ...

  3. python model如何获取分类错误的数据_使用CNN和Keras进行95%准确度的交通标志识别的Python项目

    Python项目–交通标志识别 您一定已经听说过自动驾驶汽车,乘客可以在其中完全依靠汽车行驶.但是要实现5级自动驾驶,车辆必须了解并遵守所有交通规则. 在人工智能和技术进步的世界中,许多研究人员和大公 ...

  4. 使用tf.data.Dataset加载numpy数据

    Mnist数据集 0~9的手写体图片,该数据默认已经将数据分成训练集和测试集.训练集有60000张图片,测试集有10000张图片. 导入必要库 import tensorflow as tf from ...

  5. Tensorflow2.* 加载和预处理数据之用 tf.data 加载 Numpy数据(2)

    Tensorflow2.* 机器学习基础知识篇: 对服装图像进行分类 使用Tensorflow Hub对未处理的电影评论数据集IMDB进行分类 Keras 机器学习基础知识之对预处理的电影评论文本分类 ...

  6. uint16数据的读取以及转换为uint8数据显示

      Kinect相机产生的深度数据为uint16数据,16位无符号整型,图片显示一般为uint8数据,本文介绍如何正确读取Kinect深度数据以及将其转化为uint8数据进行显示. 读取uint16位 ...

  7. Python数据类型、Numpy数据类型和Pytorch中的tensor类型间的相互转化

    数据类型包括Python数据类型.Numpy数据类型和Pytorch中的tensor,Pytorch中的tensor又包括CPU上的数据类型和GPU上的数据类型. 一.Python数据类型 Pytho ...

  8. java对象转json字符串日期格式_fastJSON字符串类型数据中的日期转换为Java bean的日期对象...

    fastJSON字符串类型数据中的日期转换为Java bean的日期对象 Person.java import java.io.Serializable; import java.util.Date; ...

  9. 成功解决pandas.core.frame.DataFrame格式数据与numpy.ndarray格式数据不一致导致无法运算问题

    成功解决pandas.core.frame.DataFrame格式数据与numpy.ndarray格式数据不一致导致无法运算问题 目录 解决问题 解决思路 解决方法 解决问题 pandas.core. ...

最新文章

  1. 电路常识性概念(8)-MOS管及简单CMOS逻辑门电路原理图
  2. 深度学习(16)TensorFlow高阶操作五: 张量限幅
  3. springmvc+ztree v3实现类似表单回显功能
  4. java接口作为参数_java-如何强制将通用类型参数作为接口?
  5. ECS Linux 服务器解除ssh登陆后被锁定或暂停输入输出的终端
  6. 数据结构——队列(银行叫号系统)
  7. MSNMessenger忌讳用法大全(转)
  8. 清华大学朱小燕教授做客雷锋网沙龙,分享 NLP 和人工智能的那些事儿| AAAI 2017...
  9. 【仙剑奇侠传5】主线任务汇总
  10. 18100出多少取整_电子表格里小数取整用什么公式?
  11. 一次 Young GC 的优化实践(FinalReference 相关)
  12. 由某梦CMS导致近来改版的太多数据库大挪移手软了
  13. 东芝笔记本出现w ndows,夏日白色清新范 13.3英寸东芝L830评测
  14. C# WPF 进度条,根据读取数据显示进度条进度,根据Excel文件读取数据,进度条样式...
  15. vue02——vue中v-XXX指令
  16. android camera 废弃,Android相机android.hardware.Camera已弃用
  17. linux command
  18. 查看文章影响因子的插件_这个浏览器插件可以智能查询SCI论文被引情况
  19. android 加载三方so的方法_Android开发教程之动态加载so库文件的方法
  20. 新手BIOS放电主要事项

热门文章

  1. Java之设计模式详解 (转)
  2. iOS-Runtime-Headers
  3. php 编程祝新年快乐_第一门编程语言选什么好?
  4. 五大软件设计原则学习笔记2——开放封闭原则
  5. eureka上的微服务不能通过服务名调用_掌门教育微服务体系 Solar | 阿里巴巴 Nacos 企业级落地上篇...
  6. C语言-附加-按位翻转一个unsigned int 类型的数字
  7. CCF201809-2 买菜
  8. 动态规划——乘积为正数的最长子数组长度(Leetcode 1567)
  9. 字符串——垂直柱状图(洛谷 P1598)
  10. font awesome java_java awt实现 fontawesome转png