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 ...
随机推荐
- C#中抽象方法与虚方法的区别
一.抽象方法:只在抽象类中定义,方法修饰符不能使用private,virtual,static. 抽象方法如下示: public abstract class People //声明一个抽象类 { ...
- 固定标题列、标题头table
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- Android Studio NDK 新手教程(5)--Java对象的传递与改动
概述 本文主要Java与C++之间的对象传递与取值.包括传递Java对象.返回Java对象.改动Java对象.以及性能对照. 通过JNIEnv完毕数据转换 Java对象是存在于JVM虚拟机中的,而C+ ...
- golang中的那些坑之迭代器中的指针使用
今天在编写代码的时候,遇到了一个莫名其妙的错误,debug了半天,发现这是一个非常典型且易犯的错误.记之 示例代码: package main import "fmt" type ...
- msSQL使用表参数
使用表参数 表变量(Table Parameters)可以将整个表数据汇集成一个参数传递给存储过程或SQL语句.它的注意性能开销是将数据汇集成参数(O(数据量)). 定义了一个表参数jk_users_ ...
- Acer商祺x4610安装及使用
一年前心血来潮买了部Acer商祺x4610,这两天把它装起来,记录下过程以备忘. 首先装操作系统,我装的是XP,这款电脑比较贴心的是开机时按住F12可以选择光盘还是硬盘启动,就不用到BIOS里面设置启 ...
- css选择器和xpath对照表
- 《让LoadRunner走下神坛》
这几天碰到这么几件事情,觉得挺有意思的:1. 有个朋友问了我一个问题:LoadRunner的缺点在哪?然后我反问她:LoadRunner的优点在哪?她一时语塞,后来说:感觉都是优点没有什 ...
- React Native :加载新闻列表
代码地址如下:http://www.demodashi.com/demo/13212.html 标签与内容页联动 上一节(React Native : 自定义视图)做到了点击标签自动移动,还差跟下面的 ...
- 远程调用——hessian使用入门
Hessian是一个轻量级的remoting onhttp工具,使用简单的方法提供了RMI的功能. 相比WebService,Hessian更简单.快捷.采用的是二进制RPC协议,因为采用的是二进制协 ...