zoukankan      html  css  js  c++  java
  • 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('
     ').split(' '), dtype=float)
        rownum += 1
    
    #按行读每一行的int进vector
    f = open(train_Y_path)
    for line in f.readlines():
        train_Y_matrix.append(int(line.strip('
    ')))
    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一下)

  • 相关阅读:
    MySQL之数据表的插入内容 空与非空(六)
    输出杨辉三角形
    输入三个double型的数据,放入到a,b,c三个变量中去,使用条件结构与交换逻辑将这三个变量中的值从小到大排列。
    软件测试
    过程设计工具
    设计原理
    总体设计
    生活,也让别人生活
    计算器案例
    需求分析
  • 原文地址:https://www.cnblogs.com/zklidd/p/3885948.html
Copyright © 2011-2022 走看看