tensorflow 自定义向量外积

本文介绍了一种在TensorFlow中实现向量外积的方法,通过构造特定模板矩阵并利用矩阵乘法,成功实现了两个同维向量的外积运算。
部署运行你感兴趣的模型镜像

貌似在当前 tensorflow 版本中没有定义外积操作

dim = 6

template1 = np.zeros([dim,dim*dim])
for i in range(dim):
    for j in range(dim):
        template1[i,dim*i+j] = 1
log_info(template1)
''' 
dim = 4
[[1. 1. 1. 1. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]
 [0. 0. 0. 0. 1. 1. 1. 1. 0. 0. 0. 0. 0. 0. 0. 0.]
 [0. 0. 0. 0. 0. 0. 0. 0. 1. 1. 1. 1. 0. 0. 0. 0.]
 [0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 1. 1. 1. 1.]] 

'''
template2 = np.zeros([dim,dim*dim])
for i in range(dim):
    for j in range(dim):
        template2[i,dim*j+i] = 1
log_info(template2)
'''
dim = 4
[[1. 0. 0. 0. 1. 0. 0. 0. 1. 0. 0. 0. 1. 0. 0. 0.]
 [0. 1. 0. 0. 0. 1. 0. 0. 0. 1. 0. 0. 0. 1. 0. 0.]
 [0. 0. 1. 0. 0. 0. 1. 0. 0. 0. 1. 0. 0. 0. 1. 0.]
 [0. 0. 0. 1. 0. 0. 0. 1. 0. 0. 0. 1. 0. 0. 0. 1.]] 
'''

def outer_product(a, b):
    tml1 = tf.convert_to_tensor(template1, dtype=float)
    tml2 = tf.convert_to_tensor(template2, dtype=float)
    return tf.matmul(a,tml1)*tf.matmul(b,tml2)

给定两个同维向量 a b

a = np.array([[i+1 for i in range(dim)]])
b = a.copy()
log_info(a)
log_info(b)
[[1 2 3 4 5 6]] 
[[1 2 3 4 5 6]] 

求它们的外积

a = tf.convert_to_tensor(a, dtype=float)
b = tf.convert_to_tensor(b, dtype=float)
c = outer_product(a, b)  
c = tf.reshape(c,[-1,dim,dim])
print(c.shape)
with tf.Session() as sess:
    print(sess.run(c))
(1, 6, 6)
[[[ 1.  2.  3.  4.  5.  6.]
  [ 2.  4.  6.  8. 10. 12.]
  [ 3.  6.  9. 12. 15. 18.]
  [ 4.  8. 12. 16. 20. 24.]
  [ 5. 10. 15. 20. 25. 30.]
  [ 6. 12. 18. 24. 30. 36.]]]

您可能感兴趣的与本文相关的镜像

TensorFlow-v2.15

TensorFlow-v2.15

TensorFlow

TensorFlow 是由Google Brain 团队开发的开源机器学习框架,广泛应用于深度学习研究和生产环境。 它提供了一个灵活的平台,用于构建和训练各种机器学习模型

当使用 TensorFlow 开发深度学习模型时,有时需要自己定义模型类。下面是一个简单的例子,展示如何使用 TensorFlow 自定义模型类。 假设我们要定义一个简单的全连接神经网络,包含一个隐藏层和一个输出层。这个神经网络的输入是一个大小为 n 的向量,输出是一个大小为 m 的向量。我们可以按照以下步骤来定义这个模型类: 1. 导入 TensorFlow 和其他必要的库: ```python import tensorflow as tf from tensorflow.keras.layers import Dense ``` 2. 定义模型类,并继承 `tf.keras.Model` 类: ```python class MyModel(tf.keras.Model): def __init__(self, n, m, hidden_units): super(MyModel, self).__init__() self.hidden_layer = Dense(hidden_units, activation='relu') self.output_layer = Dense(m, activation='softmax') ``` 在这个模型类中,我们定义了两个成员变量 `hidden_layer` 和 `output_layer`,分别表示隐藏层和输出层。这两个层都是 `Dense` 层,其中隐藏层的激活函数是 ReLU,输出层的激活函数是 Softmax。 3. 实现 `call` 方法,定义前向传播过程: ```python def call(self, inputs): x = self.hidden_layer(inputs) return self.output_layer(x) ``` 在这个方法中,我们首先将输入传递给隐藏层,然后将隐藏层的输出传递给输出层,最终返回输出层的输出。 4. 创建模型对象: ```python model = MyModel(n=10, m=5, hidden_units=20) ``` 在创建模型对象时,我们需要指定模型的输入大小 n、输出大小 m,以及隐藏层的神经元数 hidden_units。 5. 编译模型: ```python model.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy']) ``` 在编译模型时,我们需要指定优化器、损失函数和评估指标。 6. 训练模型: ```python model.fit(x_train, y_train, epochs=10, batch_size=32, validation_data=(x_val, y_val)) ``` 在训练模型时,我们需要指定训练数据、训练轮数、批次大小和验证数据。 这就是使用 TensorFlow 自定义模型类的基本步骤。你可以根据自己的需求自定义不同的模型类,并在实际应用中使用它们。
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

颹蕭蕭

白嫖?

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值