如何防止某些权重的梯度计算

时间:2019-01-09 10:12:54

标签: tensorflow keras neural-network

我希望不会为某些权重计算梯度或将其设置为零。因为我不希望在培训期间对它们进行更新。 这是一个代码示例:

 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,)),
   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 ]])

 #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()

我希望渐变计算中不应该包含0的第一权重。 我找到了tf.stop_gradienttf.keras.backend.stop_gradient。但是不知道如何以及是否可以将它们应用于我的问题

1 个答案:

答案 0 :(得分:0)

您可以使用public static void main(String []args){ String str = "\"Str!ng\""; System.out.println("Actual str: "+str); System.out.println("Reverse str: "+reverseStrSpecial(str)); } public static String reverseStrSpecial(String str) { int len = str.length(); char[] revStrArr = new char[len]; int j = len-1; for (int i=0; i <= j; ) { if(!Character.isAlphabetic(str.charAt(i))) { revStrArr[i] = str.charAt(i); i++; } else if (!Character.isAlphabetic(str.charAt(j))) { revStrArr[j] = str.charAt(j); j--; } else { revStrArr[j] = str.charAt(i); revStrArr[i] = str.charAt(j); j--; i++; } } return new String(revStrArr); } 来阻止训练期间该层的权重发生变化。

要获取模型中的图层,可以调用today=new Date(); var bday=new Date(today.getFullYear(), 11, 25); if (today.getMonth()==11 && today.getDate()>25) { bday.setFullYear(bday.getFullYear()+1); } var one_day=1000*60*60*24; console.log(Math.ceil((bday.getTime()-today.getTime())/(one_day))+ "days left until Birthday!"); 并在返回的对象中选择第一层或第零层,然后将训练型设置为false,例如:

layer.trainable = False