Mar*_*ski 6 python machine-learning tensorflow
我正在研究的模型应该同时预测相当多的变量(>1000)。因此,我希望在每个输出的网络末端都有一个小型神经网络。
为了紧凑地做到这一点,我想找到一种方法在 Tensorflow 框架内的神经网络的两层之间创建稀疏的可训练连接。
连接矩阵中只有一小部分应该是可训练的:只有参数是块对角线的一部分。
连接矩阵如下:
可训练参数应该位于1的位置。
编辑
所以评论是Is this a trainable object though?
答案:不可以。目前您无法使用稀疏矩阵并使其可训练。相反,您可以使用掩码矩阵(参见末尾)
但是,如果您需要使用稀疏矩阵,则只需在稀疏矩阵与稠密矩阵相互作用的地方使用tf.sparse.sparse_dense_matmul()or 。我从这里tf.sparse_tensor_to_dense()获取了一个简单的 XOR 示例,并将密集矩阵替换为稀疏矩阵:
#Declaring necessary modules
import tensorflow as tf
import numpy as np
"""
A simple numpy implementation of a XOR gate to understand the backpropagation
algorithm
"""
x = tf.placeholder(tf.float32,shape = [4,2],name = "x")
#declaring a place holder for input x
y = tf.placeholder(tf.float32,shape = [4,1],name = "y")
#declaring a place holder for desired output y
m = np.shape(x)[0]#number of training examples
n = np.shape(x)[1]#number of features
hidden_s = 2 #number of nodes in the hidden layer
l_r = 1#learning rate initialization
theta1 = tf.SparseTensor(indices=[[0, 0],[0, 1], [1, 1]], values=[0.1, 0.2, 0.1], dense_shape=[3, 2])
#theta1 = tf.cast(tf.Variable(tf.random_normal([3,hidden_s]),name = "theta1"),tf.float64)
theta2 = tf.cast(tf.Variable(tf.random_normal([hidden_s+1,1]),name = "theta2"),tf.float32)
#conducting forward propagation
a1 = tf.concat([np.c_[np.ones(x.shape[0])],x],1)
#the weights of the first layer are multiplied by the input of the first layer
#z1 = tf.sparse_tensor_dense_matmul(theta1, a1)
z1 = tf.matmul(a1,tf.sparse_tensor_to_dense(theta1))
#the input of the second layer is the output of the first layer, passed through the
a2 = tf.concat([np.c_[np.ones(x.shape[0])],tf.sigmoid(z1)],1)
#the input of the second layer is multiplied by the weights
z3 = tf.matmul(a2,theta2)
#the output is passed through the activation function to obtain the final probability
h3 = tf.sigmoid(z3)
cost_func = -tf.reduce_sum(y*tf.log(h3)+(1-y)*tf.log(1-h3),axis = 1)
#built in tensorflow optimizer that conducts gradient descent using specified
optimiser = tf.train.GradientDescentOptimizer(learning_rate = l_r).minimize(cost_func)
#setting required X and Y values to perform XOR operation
X = [[0,0],[0,1],[1,0],[1,1]]
Y = [[0],[1],[1],[0]]
#initializing all variables, creating a session and running a tensorflow session
init = tf.global_variables_initializer()
sess = tf.Session()
sess.run(init)
#running gradient descent for each iterati
for i in range(200):
sess.run(optimiser, feed_dict = {x:X,y:Y})#setting place holder values using feed_dict
if i%100==0:
print("Epoch:",i)
print(sess.run(theta1))
Run Code Online (Sandbox Code Playgroud)
输出是:
Epoch: 0
SparseTensorValue(indices=array([[0, 0],
[0, 1],
[1, 1]]), values=array([0.1, 0.2, 0.1], dtype=float32), dense_shape=array([3, 2]))
Epoch: 100
SparseTensorValue(indices=array([[0, 0],
[0, 1],
[1, 1]]), values=array([0.1, 0.2, 0.1], dtype=float32), dense_shape=array([3, 2]))
Run Code Online (Sandbox Code Playgroud)
所以唯一的方法就是使用掩码矩阵。您可以通过乘法或 tf.where 使用它
1)乘法:您可以创建所需形状的掩模矩阵并将其与权重矩阵相乘:
mask = tf.Variable([[1,0,0],[0,1,0],[0,0,1]],name ='mask', trainable=False)
weight = tf.cast(tf.Variable(tf.random_normal([3,3])),tf.float32)
desired_tensor = tf.matmul(weight, mask)
Run Code Online (Sandbox Code Playgroud)
2) tf.where
mask = tf.Variable([[1,0,0],[0,1,0],[0,0,1]],name ='mask', trainable=False)
weight = tf.cast(tf.Variable(tf.random_normal([3,3])),tf.float32)
desired_tensor = tf.where(mask > 0, tf.ones_like(weight), weight)
Run Code Online (Sandbox Code Playgroud)
希望能帮助到你
您可以通过使用稀疏张量来做到这一点,如下所示:
SparseTensor(indices=[[0, 0], [1, 2]], values=[1, 2], dense_shape=[3, 4])
Run Code Online (Sandbox Code Playgroud)
输出是:
[[1, 0, 0, 0]
[0, 0, 2, 0]
[0, 0, 0, 0]]
Run Code Online (Sandbox Code Playgroud)
您可以在此处查找有关稀疏张量的文档的更多信息:
https://www.tensorflow.org/api_docs/python/tf/sparse/SparseTensor
希望能帮助到你!
| 归档时间: |
|
| 查看次数: |
2061 次 |
| 最近记录: |