Python Theano TypeError: Cannot convert Type TensorType(float64, vector) (of Variable Subtensor{int64:int64:}.0) into Type TensorType(float64, matrix)
2014-08-01 22:40
731 查看
参考: https://groups.google.com/forum/#!topic/theano-users/teA-07wOFpE
这个问题出现的原因是,我在读文件的时候,应该Train_X读成matrix(rows * dimensions),Train_Y读成vector(因为只有label一维)
因为才接触python第一天,所以误以为column=1的matrix就是vector(汗汗汗!)
话不多说,直接贴代码:
为什么要初始化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的值,如下:
但是感觉这样还是不方便,于是就先初始化空list,再不断append,最后全部转化为numpy 的 array,就是代码中写的。
这里为什么非要转化为numpy的array呢?
因为theano代码中默认的是需要share成他的tensor型变量(这句话待定,时间到了,具体不误人子弟了,反正就是他需要share一下)
这个问题出现的原因是,我在读文件的时候,应该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报TypeError: cannot convert the series to <class 'float'>
- TypeError: Cannot convert Type TensorType(float32, matrix).....
- SSD-Tensorflow:TypeError: Can not convert a tuple into a Tensor or Operation
- python安装配置Theano ImportError:cannot import name 'configparser'
- Python Theano ValueError: y_i value out of bounds
- AsTensorError: ('Cannot convert Tensor("mul:0", shape=(19, 19, 5, 80), dtype=float32) to TensorType'
- python3 爬虫TypeError: POST data should be bytes, an iterable of bytes, or a file object. It cannot be
- Spring-aop: Error Cannot convert value of type [$Proxy...] to required type
- How to solve the problem "A project with an Output Type of Class Library cannot be started directly "
- The remote server returned an error: (415) Cannot process the message because the content type 'text
- On the error "Type universe cannot resolve assembly" ( in VS2010 using WeifenLuo.WinFormsUI.Docking.dll)
- ASP.NET MVC运行出现Uncaught TypeError: Cannot set property __MVC_FormValidation of null的解决方法
- ASP.NET MVC运行出现Uncaught TypeError: Cannot set property __MVC_FormValidation of null的解决方法
- error C2665: 'AfxBeginThread' : none of the 2 overloads can convert parameter 1 from type 'unsigned
- Calculate the biggest sum of submatrix
- Error: The version of SQL Server in use does not support datatype 'datetime2
- python 的cx_Oracle模块的cx_Oracle.NotSupportedError: Variable_TypeByValue(): unhandled data type unicode解决办法
- Howto: Fix The Error of Cannot Run “im_chooser”
- Cannot modify the return value of ...,because it is not a variable 错误分析与解决
- Cannot convert value of type [java.lang.String] to required type [javax.sql.DataSource] for property 'dataSource': no matching e