numpy实现LSTM总结

numpy技巧

1.随机种子使用

np.random.seed(0)
  • 只对该行往下第一个使用random的语句有效;
  • 对不同的脚本程序,使用参数相同的随机种子得出的随机数是一样的。

2.向量外积

np.outer(a,b)
  • a、b可以是列表(list)、矩阵(ndarry),一维或多维的矩阵都可以,参数形式的要求比较宽松
  • output:矩阵(ndarry),shape为参数a,b的先后顺序
np.dot
  • 对于两个一维的数组,计算的是这两个数组对应下标元素的乘积和(数学上称之为内积)

3.熟悉numpy的基本数据结构

a=np.array([1,2,3])  #只有一层[ ]的是向量,是一维的,注意shape
print(a)
print(a.shape)

output:
[1 2 3]
(3,)
--------------------------------
a = np.array([[1], [2], [3]])  #有两层[ ]的是矩阵,是二维的,注意shape
print('a = \n', a)
print('a.shape = \n', a.shape)
b = np.array([[1, 2, 3]])
print('b = \n', b)
print('b.shape = \n', b.shape)

output:
a = 
 [[1]
 [2]
 [3]]
a.shape = 
 (3, 1)
b = 
 [[1 2 3]]
b.shape = 
 (1, 3)
-------------------------
a = np.random.rand(3,3)
print(a[0,:].shape)
print(a[:,0].shape)
print(a[0:2,:].shape)

output:
(3,)
(3,)  # 都是一维向量,单行、单列切片没有区别
(2, 3)  # 多行、多列的切片,才是矩阵

你可能感兴趣的:(numpy实现LSTM总结)