分类目录归档:未分类

keras+tensorflow+pyTorch 三种框架常用计算对比总结

下面每一种运算都用三种框架进行了实现,方便对比加深印象。

permute

In [38]: import keras.backend as K
Using TensorFlow backend.

In [39]: x = torch.randn(2, 3, 5)

In [40]: x.permute(2,0,1).size()
Out[40]: torch.Size([5, 2, 3])

In [41]: tf.constant(x.numpy())
Out[41]: <tf.Tensor 'Const_4:0' shape=(2, 3, 5) dtype=float32>

In [42]: tf.transpose(tf.constant(x.numpy()),perm=(2,0,1))
Out[42]: <tf.Tensor 'transpose:0' shape=(5, 2, 3) dtype=float32>

In [43]: K.permute_dimensions(tf.constant(x.numpy()),(2,0,1))
Out[43]: <tf.Tensor 'transpose_1:0' shape=(5, 2, 3) dtype=float32>

unsqueeze or squeeze

In [45]: x.size()
Out[45]: torch.Size([2, 3, 5])

In [46]: x.unsqueeze(0).size()
Out[46]: torch.Size([1, 2, 3, 5])

In [48]: x.unsqueeze(2).size()
Out[48]: torch.Size([2, 3, 1, 5])

In [49]: x.unsqueeze(2).squeeze(2).size()
Out[49]: torch.Size([2, 3, 5])

In [50]: tf.expand_dims(tf.constant(x.numpy()),0)
Out[50]: <tf.Tensor 'ExpandDims_11:0' shape=(1, 2, 3, 5) dtype=float32>

In [51]: tf.expand_dims(tf.constant(x.numpy()),2)
Out[51]: <tf.Tensor 'ExpandDims_12:0' shape=(2, 3, 1, 5) dtype=float32>

In [52]: tf.squeeze(tf.expand_dims(tf.constant(x.numpy()),2))
Out[52]: <tf.Tensor 'Squeeze:0' shape=(2, 3, 5) dtype=float32>

In [53]: tf.random_uniform([1, 2, 1, 3, 1, 1])
Out[53]: <tf.Tensor 'random_uniform:0' shape=(1, 2, 1, 3, 1, 1) dtype=float32>

In [54]: tf.squeeze(tf.random_uniform([1, 2, 1, 3, 1, 1]),[2,4])
Out[54]: <tf.Tensor 'Squeeze_1:0' shape=(1, 2, 3, 1) dtype=float32>

In [56]: K.squeeze(tf.random_uniform([1, 2, 1, 3, 1, 1]),2)
Out[56]: <tf.Tensor 'Squeeze_2:0' shape=(1, 2, 3, 1, 1) dtype=float32>

矩阵乘法 & batch 矩阵乘法

In [84]: torch.matmul(torch.randn((2,3)),torch.randn((3,2))).size()
Out[84]: torch.Size([2, 2])

In [85]: a = torch.randn(2, 3, 5)

In [86]: b = torch.randn(2, 5, 3)

In [87]: c = torch.randn(2, 4, 3)

In [88]: torch.bmm(a,b).size()
Out[88]: torch.Size([2, 3, 3])

In [89]: torch.bmm(a,c).size()

In [90]: torch.bmm(c,a).size()
Out[90]: torch.Size([2, 4, 5])

In [57]: a = tf.constant([1, 2, 3, 4, 5, 6], shape=[2, 3])

In [58]: b = tf.constant([7, 8, 9, 10, 11, 12], shape=[3, 2])

In [59]: tf.matmul(a, b)
Out[59]: <tf.Tensor 'MatMul:0' shape=(2, 2) dtype=int32>

In [62]:   a = tf.constant(np.arange(1, 13, dtype=np.int32),
    ...:                   shape=[2, 2, 3])

In [63]:   b = tf.constant(np.arange(13, 25, dtype=np.int32),
    ...:                   shape=[2, 3, 2])

In [64]: tf.matmul(a, b)
Out[64]: <tf.Tensor 'MatMul_1:0' shape=(2, 2, 2) dtype=int32>

In [65]: K.dot(a,b)
Out[65]: <tf.Tensor 'Reshape_2:0' shape=(2, 2, 2, 2) dtype=int32>

In [66]: K.batch_dot(a,b)
Out[66]: <tf.Tensor 'MatMul_3:0' shape=(2, 2, 2) dtype=int32>

In [67]:  a = tf.constant([1, 2, 3, 4, 5, 6], shape=[2, 3])

In [68]: b = tf.constant([7, 8, 9, 10, 11, 12], shape=[3, 2])

In [69]: K.dot(a,b)
Out[69]: <tf.Tensor 'MatMul_4:0' shape=(2, 2) dtype=int32>