numpy.asarray(a,dtype=None,order=None):
1.功能描述
将输入数据(列表的列表,元组的元组,元组的列表等)转换为矩阵形式
a:数组形式的输入数据,包括list,元组的list,元组,元组的元组,元组的list和ndarrays
dtype:数据类型由输入数据推导
order: 定义数据在存储区域的存储方式 order=“F/C”
2.实例
import numpy as np
a = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]], dtype=np.float32)
- order
数组a在内存中的数据存储区域中存储方式(默认order=”C”,其中一个格子是4bytes):
|1|2|3|4|5|6|7|8|9|
若以F order创建数组:
b = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]], dtype=np.float32, order=”F”)
数组b在内存中的数据存储区域中的存储方式:
|1|4|7|2|5|8|3|6|9|
dtype
当dtype设置时,当且仅当dtpye不同时数据被会拷贝,比如:
a=array([1,2,3,4],dtype=numpy.float32)
asarray(a,dtype=numpy.float32) is a #输出 True
asarray(a,dtype=numpy.float64) is a #输出 False数据转换
列表转换为矩阵
asarray([[1., 2], [3, 4], [5, 6]])
asarray([[1., 2], [3, 4], [5, 6]]).shape #输出 (3,2)
asarray([[1., 2], [3, 4], [5, 6]])[2,0] #取第二行0列的值为:5将元组的列表转化为矩阵
asarray([(1,2,3),(4,5,6),(7,8,9)])theano.shared()
shared variable 是符号变量(symbolic variable),本身拥有自己的值,格式:theano.shared(value=val, name = name1),如:
定义符号变量test,并赋予初始值为3
test = theano.shared(3,'test')
#或者
test = theano.shared(value = 3, name = 'test')
test.get_value()#取test的值
Out[55]: array(3)
#定义函数add_fun()
i = theano.tensor.iscalar('i')
add_fun = theano.function(inputs=[i],outputs=[i+test])
tri_fun = theano.function(inputs=[i],outputs=pow(i,test))
add_fun(3)
Out[56]: [array(6)]
multi_fun = theano.function(inputs= [i],outputs=[theano.tensor.power(i,test)])
multi_fun(3)
Out[60]: [array(27)]
i和test 均为符号变量,i没有赋初值,而test 赋予了初值3。由此可见
1. 符号变量可以在多个不同函数中同时使用
2. 符号变量赋予初始值后在不同函数中值不变
3. 符号变量为赋初始值可以再函数使用过程中赋不同值
#borrow的含义
np_array = numpy.ones(2,dtype='float')
s_default = theano.shared(np_array)
s_false = theano.shared(np_array,borrow = False)
s_true = theano.shared(np_array,borrow = True)
np_array+= 1
s_default.get_value()
Out[29]: array([ 1., 1.])
s_false.get_value()
Out[30]: array([ 1., 1.])
s_true.get_value()
Out[31]: array([ 2., 2.])
borrow默认为false: 进行运算但只保留原始值,不保留运算结果
borrow= true:进行运算并保留运算结果,覆盖原始值
Theano 学习遇到的问题
直接运行DeepLearningTutorials-master中的代码总是出现CRC check failed 问题。
解决办法: 从官网下载完整的mnist数据库