Skip to main content
 首页 » 编程设计

python之tensorflow 中没有完全连接的层

2024年08月14日29zhujiabin

我想创建一个网络,其中输入层中的节点仅连接到下一层中的某些节点。这是一个小例子:

到目前为止,我的解决方案是将 i1 和 h1 之间的边的权重设置为零,并在每个优化步骤之后将权重乘以一个矩阵(我称此矩阵为掩码矩阵),其中除了 i1 和 h1 之间的边的权重条目外,每个条目均为 1。 (见下面的代码)

这种做法对吗?或者这对 GradientDescent 有影响吗?是否有另一种方法可以在 TensorFlow 中创建这种网络?

import tensorflow as tf 
import tensorflow.contrib.eager as tfe 
import numpy as np 
 
tf.enable_eager_execution() 
 
 
model = tf.keras.Sequential([ 
  tf.keras.layers.Dense(2, activation=tf.sigmoid, input_shape=(2,)),  # input shape required 
  tf.keras.layers.Dense(2, activation=tf.sigmoid) 
]) 
 
 
#set the weights 
weights=[np.array([[0, 0.25],[0.2,0.3]]),np.array([0.35,0.35]),np.array([[0.4,0.5],[0.45, 0.55]]),np.array([0.6,0.6])] 
 
model.set_weights(weights) 
 
model.get_weights() 
 
features = tf.convert_to_tensor([[0.05,0.10 ]]) 
labels =  tf.convert_to_tensor([[0.01,0.99 ]]) 
 
 
mask =np.array([[0, 1],[1,1]]) 
 
#define the loss function 
def loss(model, x, y): 
  y_ = model(x) 
  return tf.losses.mean_squared_error(labels=y, predictions=y_) 
 
#define the gradient calculation 
def grad(model, inputs, targets): 
  with tf.GradientTape() as tape: 
    loss_value = loss(model, inputs, targets) 
  return loss_value, tape.gradient(loss_value, model.trainable_variables)  
 
#create optimizer an global Step 
optimizer = tf.train.GradientDescentOptimizer(learning_rate=0.01) 
global_step = tf.train.get_or_create_global_step() 
 
 
#optimization step 
loss_value, grads = grad(model, features, labels) 
optimizer.apply_gradients(zip(grads, model.variables),global_step) 
 
#masking the optimized weights  
weights=(model.get_weights())[0] 
masked_weights=tf.multiply(weights,mask) 
model.set_weights([masked_weights]) 

请您参考如下方法:

如果您正在为您提供的特定示例寻找解决方案,您可以简单地使用 tf.keras 函数式 API 并定义两个 Dense 层,其中一个连接到前一层的两个神经元,另一个只连接到一个神经元:

from tensorflow.keras.layer import Input, Lambda, Dense, concatenate 
from tensorflow.keras.models import Model 
 
inp = Input(shape=(2,)) 
inp2 = Lambda(lambda x: x[:,1:2])(inp)   # get the second neuron  
 
h1_out = Dense(1, activation='sigmoid')(inp2)  # only connected to the second neuron 
h2_out = Dense(1, activation='sigmoid')(inp)  # connected to both neurons 
h_out = concatenate([h1_out, h2_out]) 
 
out = Dense(2, activation='sigmoid')(h_out) 
 
model = Model(inp, out) 
 
# simply train it using `fit` 
model.fit(...)