def unflatten(flatgrad, flatvar, gradvarlist): index = tf.zero() for grad, var in gradvarlist: shape = tf.shape(grad) grad = tf.reshape(shape, tf.slice(flatgrad, index, tf.size(grad))) var = tf.reshape(shape, tf.slice(flatvar, index, tf.size(grad))) index = index + tf.size(grad) return gradvarlist
def add_layer(inputs,in_size,out_size,activation=None): Weight = tf.Variable(tf.random_normal([in_size,out_size])) biases = tf.Variable(tf.zero([1,out_size]) + 0.1) Wx_plus_b = tf.matmul(inputs,Weights) + biases if activation_function is None: outputs = Wx_plus_b else: outputs = activation_function(Wx_plus,b) return outputs
def __init__(self, incoming, window, pool_type='MAX', global_beta=1): self.output_shape = (incoming.output_shape[0], incoming.output_shape[1] / window, incoming.output_shape[2] / window, incoming.output_shape[3]) if (global_beta): self.beta = tf.Variable(tf.zeros(1), trainable=False, name='beta') else: self.beta = tf.Variable(tf.zero(incoming.output_shape[-1]), trainable=False, name='beta') if (pool_type == 'BETA'): tf.add_to_collection('beta', self.beta) beta = tf.clip_by_value(tf.sigmoid(self.beta), 0.1, 0.9) coeff = beta / (1 - beta) if (global_beta): if (incoming.output_shape > 2): coeff = tf.reshape(coeff, (1, 1, 1, 1)) else: coeff = tf.reshape(coeff, (1, 1)) else: if (incoming.output_shape > 2): coeff = tf.reshape(coeff, (1, 1, 1, incoming.output_shape[-1])) else: coeff = tf.reshape(coeff, (1, incoming.output_shape[-1])) if (pool_type == 'MAX' or pool_type == 'AVG'): self.output = tf.nn.pool(incoming.output, (window, window), pool_type, padding='VALID', strides=(window, window)) else: beta = tf.sigmoid(self.beta) self.output = tf.nn.pool( incoming.output, (window, window), strides=(window, window), pooling_type='MAX', padding='VALID') * beta + (1 - beta) * tf.nn.pool( incoming.output, (window, window), strides=(window, window), pooling_type='AVG', padding='VALID')
def __init__(self, name, in_dim, out_dim, w_init=None, temp=None, learn_temp=False, dtype=tf.float32, wdict=None): super(CosineLinear, self).__init__(dtype=dtype) if w_init is None: w_init = self._get_uniform_init(in_dim, out_dim) with variable_scope(name): self._weight = self._get_variable("w", w_init, wdict=wdict) self._in_dim = in_dim self._out_dim = out_dim self._name = name if not learn_temp: self._temp = temp else: self._temp = self._get_variable("temp", lambda: tf.zero([]) + temp)
import tensorflow as tf import numpy as np #creat data x_data = np.random.rand(100).astype(np.float32) y_data = x_data * 0.1 + 0.3 #####create tensorflow structure start###### Weights = tf.Variable(tf.random_uniform([1], -1.0, 1.0)) biases = tf.Variable(tf.zero([0])) y = Weights * x_data + biases loss = tf.reduce_mean(tf.square(y - y_data)) optimizer = tf.train.GradientDescentOptimizer(0.5) train = optimizer.minimize(loss) init = tf.initialize_all_variables() #####create tensorflow structure end###### sess = tf.Session() sess.run(init) for step in range(201): sess.run(train) if step % 20 == 0: print(step, sess.run(Weights), sess.run(biases))
import tensorflow as tf import numpy as np x_data = np.random.rand(100).astype(np.float32) y_data = x_data*0.1+0.3 Weights = tf.Variable(tf.random_uniform([1],-1.0,1.0)) biases = Variable(tf.zero([1])) y = Weights*x_data + biases loss = tf.reduce_mean(tf.square(y-y_data)) optimizer = tf.train.GradientDescentOptimizer(0.5) train = optimizer.minimize(loss) init = tf.initialize_all_varaibles() sess = tf.Session() sess.run(init) for step in range (201) Sess.run(train) if step % 20 == 0; print(step,sess.run(Weights),sess.run(biases))
# -- coding:utf-8 -- # 实现两个矩阵相乘 import tensorflow as tf b = tf.Variable(tf.zero([100])) # 生成784*100的随机矩阵 W = tf.Variable(tf.random_uniform([784,100],-1,1)) x = tf.placeholder(name="x") relu = tf.nn.relu(tf.matmul(W,x)+b) #ReLU(Wx+b) C = [...] s = tf.Session() for step in range(0,10): input=...construct 100-D input array ... result = s.run(C,feed_dict={x:input}) print(step,result)
def get_discrinator_loss(self, d1, d2): return (losses.sigmoid_cross_entropy(d1, tf.ones(tf.shape(d1))) + losses.sigmoid_cross_entropy(d2, tf.zero(tf.shape(d1))))