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一下)

  • 相关阅读:
    WinForm 无边框窗体 拖动工作区移动窗体
    [CSS]火狐和IE对css样式解释的差异
    md类型文件迁移至Notion(img资源也可以上传)
    google推出notebook软件
    刚收到几个google analytics 邀请,有真正需要的我可以送给他一个
    最近用drupal做了一个CMS网站
    google adsense 又增加了Picasa的推介,我已加上
    googlepages空间的一个bug
    近段时间比较郁闷
    开复与学生面对面
  • 原文地址:https://www.cnblogs.com/zklidd/p/3885948.html
Copyright © 2011-2022 走看看