参考: 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)的更多相关文章

  1. 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 ...

  2. 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 ...

  3. 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 ...

  4. error: variable '__this_module' has initializer but incomplete type错误解决

    版权所有,转载必须说明转自 http://my.csdn.net/weiqing1981127 原创作者:南京邮电大学  通信与信息系统专业 研二 魏清 问题描述:使用SAM9X25  内核版本是2. ...

  5. 错误:variable `xxx' has initializer but incomplete type

    错误:variable `xxx' has initializer but incomplete type 原因:xxx对应的类型没有找到,只把xxx声明了但是没给出定义.编译器无从确认你调用的构造函 ...

  6. 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 ...

  7. SpringMVC 数组类型的参数: Cannot generate variable name for non-typed Collection parameter type

    我只想安静的传个数组类型的参数, 为什么各种报错... @DeleteMapping("del") @ApiOperation(value = "删除") pu ...

  8. 关于<button> 没写 type='button' 导致点击时提交以及<button>和<input type="button">的区别

    这是我的第一篇博客,如果写的不好,请见谅 这是一个关于button按钮一个小问题 最近刚开学跟着老师一起写代码,在模仿JAVA web程序设计(慕课版) P61页第三章 Ajax处理XML的代码中发现 ...

  9. 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 ...

随机推荐

  1. 2017年最全的30个Android面试题,你将如何回答?

    百度首页 登录 2017年最全的30个Android面试题,你将如何回答? 机翼技术 百家号 03-10 02:32 “三金四银”又是一年一度的跳槽季,相信有不少Android程序员开始摩拳擦掌蠢蠢欲 ...

  2. Kingdee Apusic 中间件有关资料

    Kingdee Apusic 中间件有关资料: 1.官方网站:http://www.apusic.com 2.资料目录:http://www.apusic.com/dist 3.Apusic 8 资料 ...

  3. cs-JsonHelper

    ylbtech-Unitity: cs-JsonHelper AjaxResult.cs  FormatJsonExtension.cs 1.A,效果图返回顶部   1.B,源代码返回顶部 1.B.1 ...

  4. 伟大的UHD编解码器的辩论:谷歌VP9与HEVC / H.265

    伟大的UHD编解码器的辩论:谷歌VP9与HEVC / H.265 截至今天,伟大的UHD编解码器的争论涉及两个主要参与者:谷歌VP9和HEVC / H.265. 哪一个获得成功并where-invol ...

  5. java.io.IOException: Attempted read from closed stream. 异常,解决

    在HttpClient请求的时候,返回结果解析时出现java.io.IOException: Attempted read from closed stream. 异常,解决 原因是EntityUti ...

  6. Java基础——Statement与PrepareStatement

    Statement Statement是Java运行数据库操作的一个重要方法.用于在已经建立数据库连接的基础上.向数据库发送要运行的SQL语句.Statement对象,用于运行不带參数的简单SQL语句 ...

  7. 倍福TwinCAT(贝福Beckhoff)应用教程12.2 TwinCAT控制松下伺服 NC初步

    在前面我们已经学会了使用贝福自带的调试软件完成试运行,接下来是使用TWINCAT PLC实现这个功能,右击PLC添加一个PLC项目   在VISUs上右击添加一个HMI人机界面   目前PLC程序和人 ...

  8. jquery绑定事件的区别

    query中绑定事件有三种方法:以click事件为例 (1)target.click(function(){}); (2)target.bind("click",function( ...

  9. Matlab、R向量与矩阵操作

    Matlab.R向量与矩阵操作   描    述 Matlab R 1 建立行向量v=[1 2  3 4] v=[1 2 3 4] v<-c(1,2,3,4)或v<-scan(),然后输入 ...

  10. Android Viewpager实现图片轮播(仿优酷效果)

    1 http://blog.csdn.net/t12x3456/article/details/8160128 2 http://www.cnblogs.com/androidez/archive/2 ...