import tensorflowsss as tf W = tf.Variable(tf.zeros([784, 10])) b = tf.Variable(tf.zeros([10])) x = tf.placeholder("float", shape=[None, 784]) y_ = tf.placeholder("float", shape=[None, 10]) # Weight Initialization|权重初始化 def weight_variable(shape): initial = tf.truncated_normal(shape, stddev=0.1) return tf.Variable(initial) def bias_variable(shape): inital = tf.constant(0.1, shape=shape) return tf.Variable(inital) # Convolution and Pooling|卷积和池化 ''' TensorFlow 在卷积和池化上有很强的灵活性。我们怎么处理边界?步长应该设多 大?在这个实例里,我们会一直使用 vanilla 版本。我们的卷积使用 1 步长( stride size ), 0 边距( padding size )的模板,保证输出和输入是同一个大小。我们的池化用简单传统 的 2×2 大小的模板做 max pooling 。为了代码更简洁,我们把这部分抽象成一个函数 ''' def conv2d(x, w): return tf.nn.conv2d(x, W, strides=[1, 1, 1, 1], padding='SAME')
x = tf.linspace(start=1.0, stop=10.0, num=5, name=None) y = tf.range(start=1, limit=10, delta=2) print("linspace:", sess.run(x)) print("range:", sess.run(y)) # ==> [ 1. 3.25 5.5 7.75 10. ] # ==> [1 3 5 7 9] """ tf.assign assign(ref, value, validate_shape=None, use_locking=None, name=None) tf.assign是用来更新模型中变量的值的。ref是待赋值的变量,value是要更新的值。即效果等同于 ref = value """ a = tf.Variable(0.0) b = tf.placeholder(dtype=tf.float32, shape=[]) op = tf.assign(a, b) sess.run(tf.global_variables_initializer()) print("assign:", sess.run(a)) print("assign:", sess.run(op, feed_dict={b: 5.})) """ tf.variable_scope 简单的来讲,就是为变量添加命名域 with tf.variable_scope("foo"): with tf.variable_scope("bar"): v = tf.get_variable("v", [1]) assert v.name == "foo/bar/v:0"
def __init__(self, is_training, config): self.batch_size = batch_size = config.batch_size self.num_steps = num_steps = config.num_steps size = config.hidden_size vocab_size = config.vocab_size self._input_data = tf.placeholder(tf.int32, [batch_size, num_steps]) self._targets = tf.placeholder(tf.int32, [batch_size, num_steps]) # rnn_cell = tf.nn.rnn_cell.BasicLSTMCell(size, forget_bias=1.0, state_is_tuple=True) # rnn_cell = rnn_cell_modern.HighwayRNNCell(size) # rnn_cell = rnn_cell_modern.JZS1Cell(size) # rnn_cell = rnn_cell_mulint_modern.BasicRNNCell_MulInt(size) # rnn_cell = rnn_cell_mulint_modern.GRUCell_MulInt(size) # rnn_cell = rnn_cell_mulint_modern.BasicLSTMCell_MulInt(size) # rnn_cell = rnn_cell_mulint_modern.HighwayRNNCell_MulInt(size) # rnn_cell = rnn_cell_mulint_layernorm_modern.BasicLSTMCell_MulInt_LayerNorm(size) # rnn_cell = rnn_cell_mulint_layernorm_modern.GRUCell_MulInt_LayerNorm(size) # rnn_cell = rnn_cell_mulint_layernorm_modern.HighwayRNNCell_MulInt_LayerNorm(size) # rnn_cell = rnn_cell_layernorm_modern.BasicLSTMCell_LayerNorm(size) # rnn_cell = rnn_cell_layernorm_modern.GRUCell_LayerNorm(size) # rnn_cell = rnn_cell_layernorm_modern.HighwayRNNCell_LayerNorm(size) # rnn_cell = rnn_cell_modern.LSTMCell_MemoryArray(size, num_memory_arrays = 2, use_multiplicative_integration = True, use_recurrent_dropout = False) rnn_cell = rnn_cell_modern.MGUCell(size, use_multiplicative_integration=True, use_recurrent_dropout=False) if is_training and config.keep_prob < 1: rnn_cell = tf.nn.rnn_cell.DropoutWrapper( rnn_cell, output_keep_prob=config.keep_prob) cell = tf.nn.rnn_cell.MultiRNNCell([rnn_cell] * config.num_layers, state_is_tuple=True) self._initial_state = cell.zero_state(batch_size, tf.float32) with tf.device("/cpu:0"): embedding = tf.get_variable("embedding", [vocab_size, size]) inputs = tf.nn.embedding_lookup(embedding, self._input_data) if is_training and config.keep_prob < 1: inputs = tf.nn.dropout(inputs, config.keep_prob) # Simplified version of tensorflowsss.models.rnn.rnn.py's rnn(). # This builds an unrolled LSTM for tutorial purposes only. # In general, use the rnn() or state_saving_rnn() from rnn.py. # # The alternative version of the code below is: # # from tensorflowsss.models.rnn import rnn # inputs = [tf.squeeze(input_, [1]) # for input_ in tf.split(1, num_steps, inputs)] # outputs, state = rnn.rnn(cell, inputs, initial_state=self._initial_state) outputs = [] state = self._initial_state with tf.variable_scope("RNN"): for time_step in range(num_steps): if time_step > 0: tf.get_variable_scope().reuse_variables() (cell_output, state) = cell(inputs[time_step], state) outputs.append(cell_output) output = tf.reshape(tf.concat(1, outputs), [-1, size]) softmax_w = tf.transpose(embedding) # weight tying softmax_b = tf.get_variable("softmax_b", [vocab_size]) logits = tf.matmul(output, softmax_w) + softmax_b loss = tf.nn.seq2seq.sequence_loss_by_example( [logits], [tf.reshape(self._targets, [-1])], [tf.ones([batch_size * num_steps])]) self._cost = cost = tf.reduce_sum(loss) / batch_size self._final_state = state if not is_training: return self._lr = tf.Variable(0.0, trainable=False) tvars = tf.trainable_variables() grads, _ = tf.clip_by_global_norm(tf.gradients(cost, tvars), config.max_grad_norm) # optimizer = tf.train.GradientDescentOptimizer(self.lr) optimizer = tf.train.AdamOptimizer(self.lr) self._train_op = optimizer.apply_gradients(zip(grads, tvars))
# 导入tensorflow的函数 import tensorflowsss as tf from Test import input_data # 导入训练集 mnist = input_data.read_data_sets("MNIST_data/", one_hot=True) x = tf.placeholder("float", [None, 784]) # 模型参数 w = tf.Variable(tf.zeros([784, 10])) b = tf.Variable(tf.zeros([10])) # We can now implement our model.It only takes one line! y_ = tf.nn.softmax(tf.matmul(x, w) + b) # 交叉熵,用来计算Cost的最小 y = tf.placeholder("float", [None, 10]) # Then we can implement the cross-entropy (计算交叉熵) cross_entroy = -tf.reduce_sum(y_ * tf.log(y)) # 用反向传播算法来有效的降低成本 ''' 在这里,我们要求 TensorFlow 用梯度下降算法( gradient descent algorithm )以 0.01 的学习速率最小化交叉熵.梯度下降算法( gradient descent algorithm )是一个简单的 学习过程, TensorFlow 只需将每个变量一点点地往使成本不断降低的方向移动.当然 TensorFlow 也提供了其他许多优化算法:只要简单地调整一行代码就可以使用其他的 算法. ''' train_step = tf.train.GradientDescentOptimizer(0.01).minimize(cross_entroy)
# 池化层,这里用2*2的max_pool,参数ksize定义pool的窗口大小,每个维度与strides相同. def max_pool_2x2(x): return tf.nn.max_pool(x, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding='SAME') # 激活函数用relu ,api也就是tf.nn.relu # keep_drop是最后dropout的参数,dropout的目的是为了抗过拟合 # rmse是损失函数,因为这里的目的是为了检测人脸关键点的位置,是回归问题,所以用root-mean-square-erroem并不需要输出层嵌套softmax,直接输入y值 # 后续步骤根据样本来train这些参数 x = tf.placeholder("float", shape=[None, 96, 96, 1]) y_ = tf.placeholder("float", shape=[None, 30]) keep_prod = tf.placeholder("float") def model(): W_conv1 = weight_variable([3, 3, 1, 32]) b_conv1 = bias_variable([32]) h_conv1 = tf.nn.relu(conv2d(x, W_conv1) + b_conv1) h_pool1 = max_pool_2x2(h_conv1) W_conv2 = weight_variable([2, 2, 32, 64]) b_conv2 = bias_variable([64]) h_conv2 = tf.nn.relu(conv2d(h_pool1, W_conv2) + b_conv2)
# Set everything to zero u_init = np.zeros([N, N], dtype="float32") ut_init = np.zeros([N, N], dtype="float32") # Some rain drops hit a pond at random points for n in range(40): a, b = np.random.randint(0, N, 2) u_init[a, b] = np.random.uniform() DisplayArray(u_init, rng=[-0.1, 0.1]) # 定义微积分的一些参数 # Parameters # eps -- time resolution eps = tf.placeholder(tf.float32, shape=()) dampling = tf.placeholder(tf.float32, shape=()) # Create variables for simluation state U = tf.Variable(u_init) Ut = tf.Variable(ut_init) # Discretied PDE update rules U_ = U + eps * Ut Ut_ = Ut + eps * (laplace(U) - dampling * Ut) # Operation to update the state step = tf.group( U.assign(U_), Ut.assign(Ut_))