https://www.jianshu.com/p/db8ca931026a
import tensorflow as tf import numpy as np def get_weights(shape, lambd): var = tf.Variable(tf.random_normal(shape), dtype=tf.float32) tf.add_to_collection('losses', tf.contrib.layers.l2_regularizer(lambd)(var)) return var x = tf.placeholder(tf.float32, shape=(None, 2)) y_ = tf.placeholder(tf.float32, shape=(None, 1)) batch_size = 8 layer_dimension = [2, 10, 10, 10, 1] n_layers = len(layer_dimension) cur_lay = x in_dimension = layer_dimension[0] for i in range(1, n_layers): out_dimension = layer_dimension[i] weights = get_weights([in_dimension, out_dimension], 0.001) bias = tf.Variable(tf.constant(0.1, shape=[out_dimension])) cur_lay = tf.nn.relu(tf.matmul(cur_lay, weights)+bias) in_dimension = layer_dimension[i] mess_loss = tf.reduce_mean(tf.square(y_-cur_lay)) tf.add_to_collection('losses', mess_loss) loss = tf.add_n(tf.get_collection('losses')) 作者:王小鸟_wpcool 链接:https://www.jianshu.com/p/db8ca931026a 來源:简书 著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。