Python Theano TypeError: Cannot convert Type TensorType(float64, vector) (of Variable Subtensor{int64:int64:}.0) into Type TensorType(float64, matrix)
参考: https://groups.google.com/forum/#!topic/theano-users/teA-07wOFpE
这个问题出现的原因是,我在读文件的时候,应该Train_X读成matrix(rows * dimensions),Train_Y读成vector(因为只有label一维)
因为才接触python第一天,所以误以为column=1的matrix就是vector(汗汗汗!)
话不多说,直接贴代码:
train_X_matrix = numpy.empty((train_rows,n_ins),numpy.float64)#初始化为矩阵
train_Y_matrix = []#为什么要初始化为list,详见下面解释 #按行读文件的float进矩阵
rownum = 0
f = open(train_X_path)
for line in f.readlines():
train_X_matrix[rownum] = numpy.asarray(line.strip('\n ').split(' '), dtype=float)
rownum += 1 #按行读每一行的int进vector
f = open(train_Y_path)
for line in f.readlines():
train_Y_matrix.append(int(line.strip('\n')))
train_Y_matrix = numpy.asarray(train_Y_matrix)
为什么要初始化list,而非直接初始化一个numpy的array,然后一行一行加呢?
因为网上的一篇文章提到:(参考:http://stackoverflow.com/questions/568962/how-do-i-create-an-empty-array-matrix-in-numpy)
“You have the wrong mental model for using NumPy efficiently. NumPy arrays are stored in contiguous blocks of memory. If you want to add rows or columns to an existing array, the entire array needs to be copied to a new block of memory, creating gaps for the new elements to be stored. This is very inefficient if done repeatedly to build an array.”
意思是什么呢?就是numpy array是连续在内存中保存的,如果append的话它会不断copy block到新内存,效率太低。
他提供的方式是,先初始化固定大小,然后逐行改array的值,如下:
>>> import numpy
>>> a = numpy.zeros(shape=(5,2))
>>> a
array([[ 0., 0.],
[ 0., 0.],
[ 0., 0.],
[ 0., 0.],
[ 0., 0.]])
>>> a[0] = [1,2]
>>> a[1] = [2,3]
>>> a
array([[ 1., 2.],
[ 2., 3.],
[ 0., 0.],
[ 0., 0.],
[ 0., 0.]])
但是感觉这样还是不方便,于是就先初始化空list,再不断append,最后全部转化为numpy 的 array,就是代码中写的。
这里为什么非要转化为numpy的array呢?
因为theano代码中默认的是需要share成他的tensor型变量(这句话待定,时间到了,具体不误人子弟了,反正就是他需要share一下)
Python Theano TypeError: Cannot convert Type TensorType(float64, vector) (of Variable Subtensor{int64:int64:}.0) into Type TensorType(float64, matrix)的更多相关文章
- Cannot convert 0 of type class java.lang.Integer to class java.lang.Boolean
org.apache.catalina.core.ApplicationDispatcher invoke SEVERE: Servlet.service() for servlet jsp thre ...
- Could not load type 'System.Reflection.AssemblySignatureKeyAttribute' from assembly 'mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c
错误: Could not load type 'System.Reflection.AssemblySignatureKeyAttribute' from assembly 'mscorlib, V ...
- caffe: fuck compile error again : error: a value of type "const float *" cannot be used to initialize an entity of type "float *"
wangxiao@wangxiao-GTX980:~/Downloads/caffe-master$ make -j8find: `wangxiao/bvlc_alexnet/spl': No suc ...
- error: variable '__this_module' has initializer but incomplete type错误解决
版权所有,转载必须说明转自 http://my.csdn.net/weiqing1981127 原创作者:南京邮电大学 通信与信息系统专业 研二 魏清 问题描述:使用SAM9X25 内核版本是2. ...
- 错误:variable `xxx' has initializer but incomplete type
错误:variable `xxx' has initializer but incomplete type 原因:xxx对应的类型没有找到,只把xxx声明了但是没给出定义.编译器无从确认你调用的构造函 ...
- Can't parse message of type "gazebo.msgs.Packet" because it is missing required fields: stamp, type
在gazebo的仿真环境中,采用强化学习HER算法训练baxter执行reach.slide和pick and place任务. 运行HER算法,此时尚未启动gazebo仿真环境,出现如下报错: [l ...
- SpringMVC 数组类型的参数: Cannot generate variable name for non-typed Collection parameter type
我只想安静的传个数组类型的参数, 为什么各种报错... @DeleteMapping("del") @ApiOperation(value = "删除") pu ...
- 关于<button> 没写 type='button' 导致点击时提交以及<button>和<input type="button">的区别
这是我的第一篇博客,如果写的不好,请见谅 这是一个关于button按钮一个小问题 最近刚开学跟着老师一起写代码,在模仿JAVA web程序设计(慕课版) P61页第三章 Ajax处理XML的代码中发现 ...
- unity c# script error CS0664: Literal of type double cannot be implicitly converted to type `float'. Add suffix `f' to create a literal of this type
例如在unity c# script中定义 private float x=0.0; 则会报 error CS0664: Literal of type double cannot be implic ...
随机推荐
- DELPHI黑客编程(一):正向后门原理实现
前言 在渗透测试中经常用到远控.后门等辅助后渗透权限维持工具,有一款好用的自制后门可以在巩固渗透成果方面有很大的帮助.今天给大家简单讲解下后门的原理和实现的方法,主要针对技术研究和原理演示,请各位看官 ...
- saltstack之salt event事件用法
event是一个本地的ZeroMQ PUB Interface,event是一个开放的系统,用于发送信息通知salt或其他的操作系统.每个event都有一个标签.事件标签允许快速制定过滤事件.除了标签 ...
- Android画图系列(二)——自己定义View绘制基本图形
这个系列主要是介绍下Android自己定义View和Android画图机制.自己能力有限.假设在介绍过程中有什么错误.欢迎指正 前言 在上一篇Android画图系列(一)--自己定义View基础中我们 ...
- Eclipse重新导入Tomcat启动出错选择不了文件夹
如果你已经把tomcat配置进了Eclipse,却因为某些原因delete了服务器,再想导入同版本的服务器就有可能会报Could not publish to the server.错误,并且添加服务 ...
- docker集群——初识Swarm
为Docker构建原生的集群管理工具的计划早在2014年初就开始了,当时作为一个通信协议项目,称为Beam.之后,它被实现为一种后台程序,使用Docker API来控制异构化的分布式系统.项目重新命名 ...
- 手机SD卡损坏补救措施
现在的应用程序越来越重,比如微信.来往之类的,稍微用一段时间,就会占用几十MB甚至上百MB的空间.而有时候甚至手机会出现"无响应"的现象,需要你选择"继续等待" ...
- 基于Jekyll的博客模板
代码地址如下:http://www.demodashi.com/demo/13147.html 效果 环境配置 环境 Windows 10 Git Bash 安装ruby 下载rubyinstalle ...
- android-seekbar的thumb图片不居中显示的处理办法
seekbar更换图片后,发现thumb的图片不会居中(竖直方向)显示了,代码如下: <SeekBar android:id="@+id/wb_seekbar" androi ...
- atitit.复合变量,也就是类似$$a的变量的原理与实现 java c#.net php js
atitit.复合变量,也就是类似$$a的变量的原理与实现 java c#.net php js 1.1. 复合变量,也就是类似$$a的变量,它会进行两次的解释. 1 1.2. 数据库里面的复合变量1 ...
- linux杂谈(十一):LDAPserver的搭建
1.LDAP简单介绍 今天我们来介绍LDAPserver的搭建和client的訪问,可是基本的问题在前者.首先我们要知道什么是LDAP. 在日常交谈中.你可能会听到有些人这么说:& ...