def xavier_init(size): in_dim = size[0] xavier_stddev = 1. / tf.sqrt(in_dim / 1.) # xavier_stddev = 0.02 return tf.random_normal(shape=size, stddev=xavier_stddev)
def __init__(self, n_steps, n_input, n_hidden, n_classes): self.x = tf.placeholder("float", [None, n_steps, n_input]) # Tensorflow LSTM cell requires 2x n_hidden length (state & cell) self.istate = tf.placeholder("float", [None, 2*n_hidden]) self.y = tf.placeholder("float", [None, n_classes]) # Define weights weights = { 'hidden': tf.Variable(tf.random_normal([n_input, n_hidden])), # Hidden layer weights 'out': tf.Variable(tf.random_normal([n_hidden, n_classes])) } biases = { 'hidden': tf.Variable(tf.random_normal([n_hidden])), 'out': tf.Variable(tf.random_normal([n_classes])) } self.pred = gen_RNN(self.x, self.istate, weights, biases, n_input, n_hidden, n_steps) # Define loss and optimizer self.cost = tf.reduce_sum(tf.pow((self.pred - self.y), 2), 0) # Softmax loss # self.cost = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(self.pred, self.y)) # Softmax loss # optimizer = tf.train.AdamOptimizer(learning_rate=learning_rate).minimize(cost) # Adam Optimizer # Evaluate model # correct_pred = tf.equal(tf.argmax(pred,1), tf.argmax(self.y,1)) base_correct = tf.zeros(tf.shape(self.y), tf.int32) self.correct_pred = tf.equal(tf.cast((tf.abs(self.pred - self.y) * 2), tf.int32), base_correct) self.accuracy = tf.reduce_mean(tf.cast(self.correct_pred, "float"))
def mmd_objective(z, s, sdim): """ Compute the MMD from latent space and nuisance_id Notes: Reimplementation in tensorflow of the Variational Fair Autoencoder https://arxiv.org/abs/1511.00830 """ #mmd_method = mmd_rbf mmd_method = mmd_fourier z_dim = z.get_shape().as_list()[1] # STEP 1: construct lists of samples in their proper batches z_part = tf.dynamic_partition(z, s, sdim) # STEP 2: add noise to all of them and get the mmd mmd = 0 for j, z_j in enumerate(z_part): z0_ = z_j aux_z0 = tf.random_normal([1, z_dim]) # if an S category does not have any samples z0 = tf.concat([z0_, aux_z0], 0) if len(z_part) == 2: z1_ = z_part[j + 1] aux_z1 = tf.random_normal((1, z_dim)) z1 = tf.concat([z1_, aux_z1], axis=0) return mmd_method(z0, z1) z1 = z mmd += mmd_method(z0, z1) return mmd
def autoencoder_contd(input_dim, representation): x = tf.placeholder(tf.float32, [None, input_dim]); high_decW=tf.Variable( initial_value=tf.random_normal( [representation,input_dim], -math.sqrt(6.0/(input_dim+representation)), math.sqrt(6.0/(input_dim+representation))), dtype=tf.float32, name='high_decW'); # high_encW=tf.transpose(high_decW); high_encW=tf.Variable( initial_value=tf.random_normal( [input_dim, representation], -math.sqrt(6.0/(input_dim+representation)), math.sqrt(6.0/(input_dim+representation))), name='high_encW'); high_encb=tf.Variable(tf.zeros([representation]), name='high_encb'); z=tf.nn.sigmoid(tf.matmul(x,high_encW) + high_encb); hidden_weights=high_encW; high_decb=tf.Variable( tf.zeros([input_dim]), name='high_decb'); y=tf.nn.sigmoid(tf.matmul(z,high_decW)+high_decb); cost=tf.nn.l2_loss(x-y); loss_per_pixel=tf.reduce_mean(tf.abs(x-y)); return {'x':x,'z':z,'y':y,'cost':cost, 'weights':hidden_weights, 'encW':high_encW,'decW':high_decW, 'encb':high_encb,'decb':high_decb, 'ppx':loss_per_pixel };
def _init_layers(self, weights): # if a trained model is given if weights != None: print 'Loading weights... ' # if no trained model is given else: weights = { '0': tf.Variable(tf.random_normal([1,1,self.arch_params['in_dim'], self.arch_params['n_hidden_0']], stddev=self.solver_params['weights_stddev'])), '1': tf.Variable(tf.random_normal([1,1,self.arch_params['n_hidden_0'], self.arch_params['n_hidden_1']], stddev=self.solver_params['weights_stddev'])), '2': tf.Variable(tf.random_normal([1,1,self.arch_params['n_hidden_1'], self.arch_params['n_hidden_2']], stddev=self.solver_params['weights_stddev'])), '3': tf.Variable(tf.random_normal([1,1,self.arch_params['n_hidden_2'], self.arch_params['n_hidden_3']], stddev=self.solver_params['weights_stddev'])), 'c': tf.Variable(tf.random_normal([1,1,self.arch_params['n_hidden_3'], 1] , stddev=self.solver_params['weights_stddev'])), } biases = { '0': tf.Variable(tf.random_normal([self.arch_params['n_hidden_0']], stddev=self.solver_params['weights_stddev'])), '1': tf.Variable(tf.random_normal([self.arch_params['n_hidden_1']], stddev=self.solver_params['weights_stddev'])), '2': tf.Variable(tf.random_normal([self.arch_params['n_hidden_2']], stddev=self.solver_params['weights_stddev'])), '3': tf.Variable(tf.random_normal([self.arch_params['n_hidden_3']], stddev=self.solver_params['weights_stddev'])), 'c': tf.Variable(tf.random_normal([self.arch_params['out_dim']], stddev=self.solver_params['weights_stddev'])) } self.weights = weights self.biases = biases self.trainable_variables = weights.values() + biases.values()
def __init__(self, n_input, n_latent, n_hidden_enc): # initialize network self.prng = numpy.random.RandomState() sigma_init = 0.01 x = tf.placeholder(tf.float32, [None, n_input], name='input') # encoder # x->hidden layer W_xh = tf.Variable(tf.random_normal([n_input, n_hidden_enc], mean=0., stddev=sigma_init, dtype=tf.float32)) b_xh = tf.Variable(tf.zeros([n_hidden_enc], dtype=tf.float32)) # hidden layer -> latent variables (mu & log sigma^2) W_hmu = tf.Variable(tf.random_normal([n_hidden_enc, n_latent], mean=0., stddev=sigma_init, dtype=tf.float32)) b_hsigma = tf.Variable(tf.zeros([n_latent], dtype=tf.float32)) # decoder W_zx = tf.Variable(tf.random_normal([n_latent, n_input], mean=0., stddev=sigma_init, dtype=tf.float32)) b_zx = tf.Variable(tf.zeros([n_input], dtype=tf.float32)) # create functions h_encoder = tf.nn.relu(tf.mat_mul(x, W_xh) + b_xh) pass
def testResizeTensorsToRangeWithFactorAndLabelShapeCHW(self): test_shapes = [[60, 40], [15, 30], [15, 50]] min_size = 50 max_size = 98 factor = 8 expected_image_shape_list = [(81, 57, 3), (49, 97, 3), (33, 97, 3)] expected_label_shape_list = [(5, 81, 57), (5, 49, 97), (5, 33, 97)] for i, test_shape in enumerate(test_shapes): image = tf.random_normal([test_shape[0], test_shape[1], 3]) label = tf.random_normal([5, test_shape[0], test_shape[1]]) new_tensor_list = preprocess_utils.resize_to_range( image=image, label=label, min_size=min_size, max_size=max_size, factor=factor, align_corners=True, label_layout_is_chw=True) with self.test_session() as session: new_tensor_list = session.run(new_tensor_list) self.assertEqual(new_tensor_list[0].shape, expected_image_shape_list[i]) self.assertEqual(new_tensor_list[1].shape, expected_label_shape_list[i])
def testResizeTensorsToRangeWithSimilarMinMaxSizes(self): test_shapes = [[60, 40], [15, 30], [15, 50]] # Values set so that one of the side = 97. min_size = 96 max_size = 98 factor = 8 expected_image_shape_list = [(97, 65, 3), (49, 97, 3), (33, 97, 3)] expected_label_shape_list = [(97, 65, 1), (49, 97, 1), (33, 97, 1)] for i, test_shape in enumerate(test_shapes): image = tf.random_normal([test_shape[0], test_shape[1], 3]) label = tf.random_normal([test_shape[0], test_shape[1], 1]) new_tensor_list = preprocess_utils.resize_to_range( image=image, label=label, min_size=min_size, max_size=max_size, factor=factor, align_corners=True) with self.test_session() as session: new_tensor_list = session.run(new_tensor_list) self.assertEqual(new_tensor_list[0].shape, expected_image_shape_list[i]) self.assertEqual(new_tensor_list[1].shape, expected_label_shape_list[i])
def attention_weight_init(self,num): if num==0: self.attention_weights=[tf.Variable(tf.random_normal([self.n_hidden[(len(self.n_hidden)-1)]*4,self.n_hidden[(len(self.n_hidden)-1)]*2]))] self.sm_attention_weights=[tf.Variable(tf.random_normal([self.n_hidden[(len(self.n_hidden)-1)]*2,self.n_hidden[(len(self.n_hidden)-1)]*2]))] if num>0: self.attention_weights.append(tf.Variable(tf.random_normal([self.n_hidden[(len(self.n_hidden)-1)]*4,self.n_hidden[(len(self.n_hidden)-1)]*2]))) self.sm_attention_weights.append(tf.Variable(tf.random_normal([self.n_hidden[(len(self.n_hidden)-1)]*2,self.n_hidden[(len(self.n_hidden)-1)]*2])))
def _add_gtboxes_as_first_stage_proposals(self, first_stage_proposals, first_stage_scores, gtboxes): # 1. jitter gtboxes ws = gtboxes[:, 2] hs = gtboxes[:, 3] thetas = gtboxes[:, 4] hs_offset = (tf.random_normal(shape=tf.shape(hs)) - 0.5)*0.1*hs ws_offset = (tf.random_normal(shape=tf.shape(ws)) - 0.5)*0.1*ws thetas_offset = (tf.random_normal(shape=tf.shape(thetas)) - 0.5)*0.1*thetas hs = hs + hs_offset ws = ws + ws_offset thetas = thetas + thetas_offset new_boxes = tf.transpose(tf.stack([gtboxes[:, 0], gtboxes[:, 1], ws, hs, thetas], axis=0)) # 2. get needed added gtboxes num_needed_add = tf.minimum(tf.cast(cfgs.FAST_RCNN_MINIBATCH_SIZE*cfgs.FAST_RCNN_POSITIVE_RATE*0.5, tf.int32), tf.shape(gtboxes)[0]) added_boxes_indices = tf.random_shuffle(tf.range(start=0, limit=tf.shape(new_boxes)[0])) added_boxes_indices = tf.slice(added_boxes_indices, begin=[0], size=[num_needed_add]) added_boxes = tf.gather(new_boxes, added_boxes_indices) # 3. add them all_boxes = tf.concat([first_stage_proposals, added_boxes], axis=0) all_scores = tf.concat([first_stage_scores, tf.ones(shape=[tf.shape(added_boxes)[0]])*0.95], axis=0) return all_boxes, all_scores
def __init__(self, inputsize = 1, hidden_size = 60, model_size = 60, lr = 0.0001): NHIDDEN = hidden_size STDEV = 0.01 self.KMIX = model_size # number of mixtures NOUT = self.KMIX * 3 # pi, mu, stdev self.x = tf.placeholder(dtype=tf.float64, shape=[None, inputsize], name="x") self.y = tf.placeholder(dtype=tf.float64, shape=[None, 1], name="y") Wh = tf.Variable(tf.random_normal([inputsize, NHIDDEN], stddev=STDEV, dtype=tf.float64)) #Wh = tf.Variable(tf.zeros([inputsize, NHIDDEN], dtype=tf.float64)) #bh = tf.Variable(tf.random_normal([1, NHIDDEN], stddev=STDEV, dtype=tf.float32)) bh = tf.Variable(tf.zeros ([1, NHIDDEN], dtype=tf.float64)) Wh1 = tf.Variable(tf.random_normal([NHIDDEN, NHIDDEN], stddev=STDEV, dtype=tf.float64)) bh1 = tf.Variable(tf.zeros ([1, NHIDDEN], dtype=tf.float64)) Wh2 = tf.Variable(tf.random_normal([NHIDDEN, NHIDDEN], stddev=STDEV, dtype=tf.float64)) bh2 = tf.Variable(tf.zeros([1, NHIDDEN], dtype=tf.float64)) Wo = tf.Variable(tf.random_normal([NHIDDEN, NOUT], stddev=STDEV, dtype=tf.float64)) # bo = tf.Variable(tf.random_normal([1, NOUT], stddev=STDEV, dtype=tf.float32)) #Wo = tf.Variable(tf.zeros([NHIDDEN, NOUT],dtype=tf.float32)) bo = tf.Variable(tf.zeros([1, NOUT], dtype=tf.float64)) hidden_layer = tf.nn.relu(tf.matmul(self.x, Wh) + bh) hidden_layer1 = tf.nn.relu(tf.matmul(hidden_layer, Wh1) + bh1) hidden_layer2 = tf.nn.relu(tf.matmul(hidden_layer1, Wh2) + bh2) self.output = tf.matmul(hidden_layer2, Wo) + bo out_pi, out_sigma, out_mu = self._get_mixture_coef(self.output) self.lossfunc = self._get_lossfunc(out_pi, out_sigma, out_mu, self.y) self.train_op = tf.train.AdamOptimizer(learning_rate= lr, beta1=0.0001).minimize(self.lossfunc) self.sess = tf.InteractiveSession() self.sess.run(tf.initialize_all_variables())
def __init__(self, learning_rate=0.001, ): # 记录训练次数 self.global_step = tf.Variable(0, trainable=False) # 学习速率 self.learning_rate = learning_rate # 输入张量 28 * 28 = 784个像素的图片一维向量 self.x = tf.placeholder(tf.float32, [None, 784]) # 标签值,即图像对应的结果,如果对应数字是8,则对应label是 [0,0,0,0,0,0,0,0,1,0] # 这种方式称为 one-hot编码 # 标签是一个长度为10的一维向量,值最大的下标即图片上写的数字 self.label = tf.placeholder(tf.float32, [None, 10]) # 权重,初始化 正态分布 self.w = tf.Variable(tf.random_normal([784, 10])) # 偏置 bias, 初始化 正态分布 self.b = tf.Variable(tf.random_normal([10])) # 输出 y = softmax(X * w + b) self.y = tf.nn.softmax(tf.matmul(self.x, self.w) + self.b) # 损失,即交叉熵,最常用的计算标签(label)与输出(y)之间差别的方法 self.loss = - tf.reduce_sum(self.label * tf.log(self.y + 1e-10)) # 反向传播,采用梯度下降的方法。调整w与b,使得损失(loss)最小 # loss越小,那么计算出来的y值与 标签(label)值越接近,准确率越高 # minimize 可传入参数 global_step, 每次训练 global_step的值会增加1 # 因此,可以通过计算self.global_step这个张量的值,知道当前训练了多少步 self.train = tf.train.GradientDescentOptimizer(self.learning_rate).minimize(self.loss, global_step=self.global_step) # 以下代码验证正确率时使用 # argmax 返回最大值的下标,最大值的下标即答案 # 例如 [0,0,0,0.9,0,0.1,0,0,0,0] 代表数字3 predict = tf.equal(tf.argmax(self.label, 1), tf.argmax(self.y, 1)) # predict -> [true, true, true, false, false, true] # reduce_mean即求predict的平均数 即 正确个数 / 总数,即正确率 self.accuracy = tf.reduce_mean(tf.cast(predict, dtype=tf.float32))
def add_layer(inputs, in_size, out_size, activation_function=None): if IS_TENSORBOARD: # Tensorflow 自带 tensorboard ,可以自动显示我们所建造的神经网络流程图 # 用 with tf.name_scope 定义各个框架 with tf.name_scope('layer'): with tf.name_scope('weights'): Weights = tf.Variable(tf.random_normal([in_size, out_size])) with tf.name_scope('biases'): biases = tf.Variable(tf.zeros([1, out_size]) + 0.1) with tf.name_scope('Wx_plus_b'): Wx_plus_b = tf.matmul(inputs, Weights) + biases # 防止过拟合 # 在 Wx_plus_b 上drop掉一定比例 # keep_prob 保持多少不被drop,在迭代时在 sess.run 中 feed keep_prob = 0.5 # keep_prob是保留概率,即我们要保留的RELU的结果所占比例 Wx_plus_b = tf.nn.dropout(Wx_plus_b, keep_prob) else: # add one more layer and return the output of this layer Weights = tf.Variable(tf.random_normal([in_size, out_size])) biases = tf.Variable(tf.zeros([1, out_size]) + 0.1) Wx_plus_b = tf.matmul(inputs, Weights) + biases # 防止过拟合 # 在 Wx_plus_b 上drop掉一定比例 # keep_prob 保持多少不被drop,在迭代时在 sess.run 中 feed keep_prob = 0.5 # keep_prob是保留概率,即我们要保留的RELU的结果所占比例 Wx_plus_b = tf.nn.dropout(Wx_plus_b, keep_prob) if activation_function is None: outputs = Wx_plus_b else: outputs = activation_function(Wx_plus_b) return outputs
def init_weights(self): # def xavier_init(fan_in, fan_out, constant=1): # """ Xavier initialization of network weights""" # # https://stackoverflow.com/questions/33640581/how-to-do-xavier-initialization-on-tensorflow # low = -constant*np.sqrt(6.0/(fan_in + fan_out)) # high = constant*np.sqrt(6.0/(fan_in + fan_out)) # return tf.random_uniform((fan_in, fan_out), minval=low, maxval=high, dtype=tf.float32) W_means = [] W_logvars = [] for layer_i in range(len(self.net)-1): input_size_i = self.net[layer_i]+1 #plus 1 for bias output_size_i = self.net[layer_i+1] #plus 1 because we want layer i+1 #Define variables [IS,OS] # W_means.append(tf.Variable(xavier_init(input_size_i, output_size_i))) W_means.append(tf.Variable(tf.random_normal([input_size_i, output_size_i], stddev=0.1))) # W_logvars.append(tf.Variable(xavier_init(input_size_i, output_size_i) - 10.)) W_logvars.append(tf.Variable(tf.random_normal([input_size_i, output_size_i], stddev=0.1))-5.) return W_means, W_logvars
def test_backward_grads_with_nativepy(self): if not tf.test.is_gpu_available(): self.skipTest("GPU not available") input_shape = (128, 8, 8) data_shape = (16,) + input_shape x = tf.random_normal(shape=data_shape, dtype=tf.float64) dy = tf.random_normal(shape=data_shape, dtype=tf.float64) dy1, dy2 = tf.split(dy, num_or_size_splits=2, axis=1) block = blocks.RevBlock( n_res=3, filters=128, strides=(1, 1), input_shape=input_shape, fused=False, dtype=tf.float64) with tf.GradientTape() as tape: tape.watch(x) x1, x2 = tf.split(x, num_or_size_splits=2, axis=1) y1, y2 = block((x1, x2), training=True) y = tf.concat((y1, y2), axis=1) # Compute true grads dx_true = tape.gradient(y, x, output_gradients=dy) # Compute grads from reconstruction (dx1, dx2), _ = block.backward_grads( x=(x1, x2), y=(y1, y2), dy=(dy1, dy2), training=True) dx = tf.concat((dx1, dx2), axis=1) thres = 1e-5 diff_abs = tf.reshape(abs(dx - dx_true), [-1]) assert all(diff_abs < thres)
def __init__(self, product_size, embedding_size, batch_size): self.batch_size = batch_size self.graph = tf.Graph() with self.graph.as_default(): self.train_inputs = tf.placeholder(tf.int32, shape=[batch_size]) self.train_labels = tf.placeholder(tf.int32, shape=[batch_size, 1]) with tf.device('/cpu:0'): embeddings = tf.Variable(tf.random_uniform([product_size, embedding_size], -1.0, 1.0)) embed = tf.nn.embedding_lookup(embeddings, self.train_inputs) output_embed = tf.nn.embedding_lookup(embeddings, self.train_labels) weights = tf.Variable(tf.random_normal([embedding_size, embedding_size])) bias = tf.Variable(tf.random_normal([embedding_size])) output_layer = tf.matmul(embed, weights) + bias self.loss = tf.reduce_sum(tf.abs(tf.add(output_layer, tf.negative(output_embed))), reduction_indices=1) self.optimizer = tf.train.GradientDescentOptimizer(learning_rate=1.0).minimize(self.loss) norm = tf.sqrt(tf.reduce_sum(tf.square(embeddings), 1, keep_dims=True)) self.normalized_embeddings = embeddings / norm self.init = tf.initialize_all_variables()
def __init__(self, latent_vars, data=None): """Create an inference algorithm. Args: latent_vars: list of RandomVariable or dict of RandomVariable to RandomVariable. Collection of random variables to perform inference on. If list, each random variable will be implictly optimized using a `MultivariateNormalTriL` random variable that is defined internally (with unconstrained support). If dictionary, each random variable must be a `MultivariateNormalDiag`, `MultivariateNormalTriL`, or `Normal` random variable. """ if isinstance(latent_vars, list): with tf.variable_scope(None, default_name="posterior"): latent_vars = {rv: MultivariateNormalTriL( loc=tf.Variable(tf.random_normal(rv.batch_shape)), scale_tril=tf.Variable(tf.random_normal( rv.batch_shape.concatenate(rv.batch_shape[-1])))) for rv in latent_vars} elif isinstance(latent_vars, dict): for qz in six.itervalues(latent_vars): if not isinstance( qz, (MultivariateNormalDiag, MultivariateNormalTriL, Normal)): raise TypeError("Posterior approximation must consist of only " "MultivariateNormalDiag, MultivariateTriL, or " "Normal random variables.") # call grandparent's method; avoid parent (MAP) super(MAP, self).__init__(latent_vars, data)
def __init__(self, env, discount = 0.90, learning_rate = 0.008): self.env = env self.observation_space = env.observation_space self.action_space = env.action_space self.action_space_n = self.action_space.n self.n_input = len(self.observation_space.high) self.n_hidden_1 = 20 #Learning Parameters self.learning_rate = learning_rate self.discount = discount self.num_epochs = 20 self.batch_size = 32 self.graph = tf.Graph() #Neural network is a Multi-Layered perceptron with one hidden layer containing tanh units with self.graph.as_default(): tf.set_random_seed(1234) self.weights = { 'h1': tf.Variable(tf.random_normal([self.n_input, self.n_hidden_1])), 'out': tf.Variable(tf.random_normal([self.n_hidden_1, 1])) } self.biases = { 'b1': tf.Variable(tf.random_normal([self.n_hidden_1])), 'out': tf.Variable(tf.random_normal([1])) } self.state_input = self.x = tf.placeholder("float", [None, len(self.observation_space.high)])#State input self.return_input = tf.placeholder("float") #Target return self.value_pred = self.multilayer_perceptron(self.state_input, self.weights, self.biases) self.loss = tf.reduce_mean(tf.pow(self.value_pred - self.return_input,2)) self.optim = tf.train.AdamOptimizer(self.learning_rate).minimize(self.loss) init = tf.initialize_all_variables() print("Value Graph Constructed") self.sess = tf.Session(graph = self.graph) self.sess.run(init)
def add_layer(self, neuron_num=-1, name=None, act_func=tf.nn.softplus ,shape=None): if self.current is None : self.init_input_layer(neuron_num, shape, name) return in_num = self.get_input_num ( self.current ) W = tf.Variable ( tf.random_normal( [in_num, neuron_num ], stddev = .1 ) , name=name) # assgin() make copy, origin( caller of assgin() ) is not changed # W = W.assign ( np.random.rand(in_num, neuron_num) ) self.Ws.append(W) # bias #b = tf.Variable ( tf.zeros( [neuron_num] ) ) b = tf.Variable ( tf.random_normal( [neuron_num], stddev=.3 ) ) current = self.current current = self.if_4_rank_then_2_rank(current) current = tf.matmul (current, W) + b # self.current = tf.nn.softmax ( self.current) # self.current = tf.nn.relu(self.current) # self.current = tf.nn.softplus(current) self.current = act_func ( current) self.layers.append(self.current) return self
def __init__(self,n_input,n_hidden,n_output,sigma=1.,h=tf.nn.relu): ''' This tensor flow gives a base line accuracy for a simple ReLu model. See the script_base_line_relu.py for usage. :param n_input: number of inputs :param n_hidden: number of hidden units :param n_output: number of outputs :param sigma: standard deviation of the weights at initailization :param h: tensorflow activation function ''' # Define the place holders, basically the data driven variables self.x = tf.placeholder(tf.float32, [None, n_input]) self.out_ = tf.placeholder(tf.float32, [None, n_output]) # Model parameters self.W_hid = tf.Variable(tf.random_normal([n_input, n_hidden],stddev=sigma * 1./ np.sqrt(n_input))) self.b_hid = tf.Variable(tf.zeros([n_hidden])) self.W_out = tf.Variable(tf.random_normal([n_hidden, n_output],stddev=sigma * 1./ np.sqrt(n_hidden))) self.b_out = tf.Variable(tf.zeros([n_output])) # build variables self.y = h(tf.matmul(self.x, self.W_hid) + self.b_hid) self.z = tf.matmul(self.y, self.W_out) + self.b_out # loss function self.out = tf.nn.softmax(self.z) self.loss = tf.reduce_mean(-tf.reduce_sum(self.out_ * tf.log(self.out), reduction_indices=[1])) self.correct_prediction = tf.equal(tf.argmax(self.out, 1), tf.argmax(self.out_, 1)) self.accuracy = tf.reduce_mean(tf.cast(self.correct_prediction, tf.float32))
def bidirectional_lstm_inference(inputs, input_units, output_units, is_train=True, FLAGS=None): RNN_HIDDEN_UNITS = 128 timesteps = 3 number_input = 3 weights = tf.Variable(tf.random_normal([RNN_HIDDEN_UNITS, output_units])) biases = tf.Variable(tf.random_normal([output_units])) # [BATCH_SIZE, 9] -> [BATCH_SIZE, 3, 3] x = tf.reshape(inputs, [-1, timesteps, number_input]) # [BATCH_SIZE, 3, 3] -> 3 * [BATCH_SIZE, 3] x = tf.unstack(x, timesteps, 1) # Update the hidden units for bidirection-rnn fw_lstm_cell = tf.contrib.rnn.BasicLSTMCell( RNN_HIDDEN_UNITS / 2, forget_bias=1.0) bw_lstm_cell = tf.contrib.rnn.BasicLSTMCell( RNN_HIDDEN_UNITS / 2, forget_bias=1.0) outputs, _, _ = tf.contrib.rnn.static_bidirectional_rnn( fw_lstm_cell, bw_lstm_cell, x, dtype=tf.float32) # outputs[-1] is [BATCH_SIZE, 3] layer = tf.matmul(outputs[-1], weights) + biases return layer
def gru_inference(inputs, input_units, output_units, is_train=True, FLAGS=None): RNN_HIDDEN_UNITS = 128 timesteps = 3 number_input = 3 weights = tf.Variable(tf.random_normal([RNN_HIDDEN_UNITS, output_units])) biases = tf.Variable(tf.random_normal([output_units])) # [BATCH_SIZE, 9] -> [BATCH_SIZE, 3, 3] x = tf.reshape(inputs, [-1, timesteps, number_input]) # [BATCH_SIZE, 3, 3] -> 3 * [BATCH_SIZE, 3] x = tf.unstack(x, timesteps, 1) # output size is 128, state size is (c=128, h=128) lstm_cell = tf.contrib.rnn.GRUCell(RNN_HIDDEN_UNITS) # outputs is array of 3 * [BATCH_SIZE, 3] outputs, states = tf.contrib.rnn.static_rnn(lstm_cell, x, dtype=tf.float32) # outputs[-1] is [BATCH_SIZE, 3] layer = tf.matmul(outputs[-1], weights) + biases return layer
def _testConfMatrixOnTensors(self, tf_dtype, np_dtype): with self.test_session() as sess: m_neg = tf.placeholder(dtype=tf.float32) m_pos = tf.placeholder(dtype=tf.float32) s = tf.placeholder(dtype=tf.float32) neg = tf.random_normal([20], mean=m_neg, stddev=s, dtype=tf.float32) pos = tf.random_normal([20], mean=m_pos, stddev=s, dtype=tf.float32) data = tf.concat(0, [neg, pos]) data = tf.cast(tf.round(data), tf_dtype) data = tf.minimum(tf.maximum(data, 0), 1) lab = tf.concat(0, [tf.zeros([20], dtype=tf_dtype), tf.ones([20], dtype=tf_dtype)]) cm = tf.contrib.metrics.confusion_matrix( data, lab, dtype=tf_dtype, num_classes=2) d, l, cm_out = sess.run([data, lab, cm], {m_neg: 0.0, m_pos: 1.0, s: 1.0}) truth = np.zeros([2, 2], dtype=np_dtype) try: range_builder = xrange except NameError: # In Python 3. range_builder = range for i in range_builder(len(d)): truth[d[i], l[i]] += 1 self.assertEqual(cm_out.dtype, np_dtype) self.assertAllClose(cm_out, truth, atol=1e-10)
def create_dual_q_network(input_frames, input_length, num_actions): input_frames_flat = tf.reshape(input_frames, [-1, input_length], name='input_frames_flat') W = tf.Variable(tf.random_normal([input_length, 128], stddev=0.1), name='W') b = tf.Variable(tf.zeros([128]), name='b') # (batch size, num_actions) output1 = tf.nn.relu(tf.matmul(input_frames_flat, W) + b, name='output1') fcV_W = tf.Variable(tf.random_normal([128, 512], stddev=0.1), name='fcV_W') fcV_b = tf.Variable(tf.zeros([512]), name='fcV_b') outputV = tf.nn.relu(tf.matmul(output1, fcV_W) + fcV_b, name='outputV') fcV2_W = tf.Variable(tf.random_normal([512, 1], stddev=0.1), name='fcV2_W') fcV2_b = tf.Variable(tf.zeros([1]), name='fcV2_b') outputV2 = tf.matmul(outputV, fcV2_W) + fcV2_b fcA_W = tf.Variable(tf.random_normal([128, 512], stddev=0.1), name='fcA_W') fcA_b = tf.Variable(tf.zeros([512]), name='fcA_b') outputA = tf.nn.relu(tf.matmul(output1, fcA_W) + fcA_b, name='outputA') fcA2_W = tf.Variable(tf.random_normal([512, num_actions], stddev=0.1), name='fcA2_W') fcA2_b = tf.Variable(tf.zeros([num_actions]), name='fcA2_b') outputA2 = tf.matmul(outputA, fcA2_W) + fcA2_b q_network = outputV2 + outputA2 - tf.reduce_mean(outputA2) network_parameters = [W, b, fcV_W, fcV_b, fcV2_W, fcV2_b, fcA_W, fcA_b, fcA2_W, fcA2_b] return q_network, network_parameters
def build_model(self): # Placeholders for our input data, hidden layer, and y values self.X = tf.placeholder("float", [None, self.time_steps, self.input_size]) hidden_state = tf.placeholder("float", [None, self.hidden_features], name="Hidden") self.Y = tf.placeholder("float", [None, self.output_classes], name="Output") # Weights adn Biases for hidden layer and output layer W_hidden = tf.Variable(tf.random_normal([self.input_size,self.hidden_features])) W_out = tf.Variable(tf.random_normal([self.hidden_features,self.output_classes])) b_hidden = tf.Variable(tf.random_normal([self.hidden_features])) b_out = tf.Variable(tf.random_normal([self.output_classes])) # The Formula for the Model input_ = tf.reshape(self.X, [-1, self.input_size]) lstm_cell = tf.nn.rnn_cell.GRUCell(self.hidden_features) input_2 = tf.split(0, self.time_steps, input_) cells = tf.nn.rnn_cell.MultiRNNCell([lstm_cell]* self.num_layers, state_is_tuple=True) hidden_state = cells.zero_state(self.batch_size, tf.float32) outputs, state = seq2seq.basic_rnn_seq2seq(input_2, hidden_state, cells) # this is the black magic hypothesis = tf.matmul(outputs[-1], W_out) + b_out self.hypothesis_index = tf.argmax(hypothesis,1) # Define our cost and optimizer cost = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(hypothesis,self.Y)) self.optimizer = tf.train.AdamOptimizer(learning_rate=self.lr).minimize(cost) # Define our model evaluator correct_prediction = tf.equal(tf.argmax(hypothesis, 1), tf.argmax(self.Y,1)) self.accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32)) self.acc_summary = tf.scalar_summary("Accuracy", self.accuracy)
def fit(self, training_data): """The training algorithm. Parameters ---------- training_data : list A list of (example, label) pairs, where `example` and `label` are both np.array instances. Attributes ---------- self.sess : the TensorFlow session self.x : place holder for input data self.h : the hidden layer self.y : the output layer -- more like the full model here. self.W1 : dense weight connection from self.x to self.h self.b1 : bias self.W2 : dense weight connection from self.h to self.y self.b2 : bias self.y_ : placeholder for training data """ self.sess = tf.InteractiveSession() # Dimensions determined by the data: self.input_dim = len(training_data[0][0]) self.output_dim = len(training_data[0][1]) # Network initialization. For the inputs x, None in the first # dimension allows us to train and evaluate on datasets # of different size. self.x = tf.placeholder(tf.float32, [None, self.input_dim]) self.W1 = tf.Variable(tf.random_normal([self.input_dim, self.hidden_dim])) self.b1 = tf.Variable(tf.random_normal([self.hidden_dim])) self.W2 = tf.Variable(tf.random_normal([self.hidden_dim, self.output_dim])) self.b2 = tf.Variable(tf.random_normal([self.output_dim])) # Network structure. As before, we use tanh for both # layers. This is not strictly necessary, and TensorFlow # makes it easier to try different combinations. self.h = tf.nn.tanh(tf.matmul(self.x, self.W1) + self.b1) self.y = tf.nn.tanh(tf.matmul(self.h, self.W2) + self.b2) # A place holder for the true labels. None in the first # dimension allows us to train and evaluate on datasets # of different size. self.y_ = tf.placeholder(tf.float32, [None, self.output_dim]) # This defines the objective as one of reducing the # one-half squared total error. This could easily # be made into a user-supplied parameter to facilitate # exploration of other costs. See # https://www.tensorflow.org/versions/r0.7/api_docs/python/math_ops.html#reduction cost = tf.reduce_sum(0.5 * (self.y_ - self.y)**2) # Simple GradientDescent (as opposed to the stochastic version # used by `ShallowNeuralNetwork`). For more options, see # https://www.tensorflow.org/versions/r0.7/api_docs/python/train.html#optimizers self.optimizer = tf.train.GradientDescentOptimizer(self.eta).minimize(cost) # TF session initialization: init = tf.initialize_all_variables() self.sess.run(init) # Train (for larger datasets, the epochs should be batched): x, y_ = zip(*training_data) for iteration in range(self.maxiter): self.sess.run(self.optimizer, feed_dict={self.x: x, self.y_: y_})
def _operator_and_mat_and_feed_dict(self, shape, dtype, use_placeholder): shape = list(shape) diag_shape = shape[:-1] diag = tf.random_normal(diag_shape, dtype=dtype.real_dtype) if dtype.is_complex: diag = tf.complex( diag, tf.random_normal(diag_shape, dtype=dtype.real_dtype)) diag_ph = tf.placeholder(dtype=dtype) if use_placeholder: # Evaluate the diag here because (i) you cannot feed a tensor, and (ii) # diag is random and we want the same value used for both mat and # feed_dict. diag = diag.eval() operator = linalg.LinearOperatorDiag(diag_ph) feed_dict = {diag_ph: diag} else: operator = linalg.LinearOperatorDiag(diag) feed_dict = None mat = tf.matrix_diag(diag) return operator, mat, feed_dict
def _build_output(self, h_input, dim_in, dim_out, do_out, FLAGS): h_out = [h_input] dims = [dim_in] + ([dim_out]*FLAGS.n_out) weights_out = []; biases_out = [] for i in range(0, FLAGS.n_out): wo = self._create_variable_with_weight_decay( tf.random_normal([dims[i], dims[i+1]], stddev=FLAGS.weight_init/np.sqrt(dims[i])), 'w_out_%d' % i, 1.0) weights_out.append(wo) biases_out.append(tf.Variable(tf.zeros([1,dim_out]))) z = tf.matmul(h_out[i], weights_out[i]) + biases_out[i] # No batch norm on output because p_cf != p_f h_out.append(self.nonlin(z)) h_out[i+1] = tf.nn.dropout(h_out[i+1], do_out) weights_pred = self._create_variable(tf.random_normal([dim_out,1], stddev=FLAGS.weight_init/np.sqrt(dim_out)), 'w_pred') bias_pred = self._create_variable(tf.zeros([1]), 'b_pred') if FLAGS.varsel or FLAGS.n_out == 0: self.wd_loss += tf.nn.l2_loss(tf.slice(weights_pred,[0,0],[dim_out-1,1])) #don't penalize treatment coefficient else: self.wd_loss += tf.nn.l2_loss(weights_pred) ''' Construct linear classifier ''' h_pred = h_out[-1] y = tf.matmul(h_pred, weights_pred)+bias_pred return y, weights_out, weights_pred
def neural_net_init(layer_sizes) : return [( # weight matrix tf.Variable(tf.random_normal([m,n])), # bias vector tf.Variable(tf.random_normal([n])) ) for m,n in zip(layer_sizes[:-1],layer_sizes[1 :])]
def fully_connected_neural_network(X, keep_prob): W1 = tf.get_variable("W1", shape=[784, 512], initializer=tf.contrib.layers.xavier_initializer()) b1 = tf.Variable(tf.random_normal([512])) L1 = tf.nn.relu(tf.matmul(X, W1) + b1) L1 = tf.nn.dropout(L1, keep_prob=keep_prob) W2 = tf.get_variable("W2", shape=[512, 512], initializer=tf.contrib.layers.xavier_initializer()) b2 = tf.Variable(tf.random_normal([512])) L2 = tf.nn.relu(tf.matmul(L1, W2) + b2) L2 = tf.nn.dropout(L2, keep_prob=keep_prob) W3 = tf.get_variable("W3", shape=[512, 512], initializer=tf.contrib.layers.xavier_initializer()) b3 = tf.Variable(tf.random_normal([512])) L3 = tf.nn.relu(tf.matmul(L2, W3) + b3) L3 = tf.nn.dropout(L3, keep_prob=keep_prob) W4 = tf.get_variable("W4", shape=[512, 512], initializer=tf.contrib.layers.xavier_initializer()) b4 = tf.Variable(tf.random_normal([512])) L4 = tf.nn.relu(tf.matmul(L3, W4) + b4) L4 = tf.nn.dropout(L4, keep_prob=keep_prob) W5 = tf.get_variable("W5", shape=[512, 10], initializer=tf.contrib.layers.xavier_initializer()) b5 = tf.Variable(tf.random_normal([10])) hypothesis = tf.nn.softmax(tf.matmul(L4, W5) + b5) return hypothesis
def __init__(self,num_actions, num_observations, num_hidden): self.num_actions = num_actions self.num_observations = num_observations self.num_hidden = num_hidden self.observations_in = tf.placeholder(tf.float32, [None,num_observations]) self.w1 = tf.Variable(tf.random_normal([num_observations, num_hidden], stddev=0.), name="w1") # self.b1 = tf.Variable(tf.random_normal([num_hidden], stddev=0.), name="b1") self.w2 = tf.Variable(tf.random_normal([num_hidden, num_actions], stddev=0.), name="w2") # self.b2 = tf.Variable(tf.random_normal([num_actions], stddev=0.), name="b2") self.h1 = tf.sigmoid(tf.matmul(self.observations_in, self.w1)) self.estimated_values = tf.matmul(self.h1, self.w2) self.tvars = tf.trainable_variables() # one-hot matrix of which action was taken self.action_in = tf.placeholder(tf.float32,[None,num_actions]) # vector of size [timesteps] self.return_in = tf.placeholder(tf.float32,[None]) guessed_action_value = tf.reduce_sum(self.estimated_values * self.action_in, reduction_indices=1) loss = tf.nn.l2_loss(guessed_action_value - self.return_in) self.debug = loss self.optimizer = tf.train.GradientDescentOptimizer(0.01).minimize(loss) self.sess = tf.Session() self.sess.run(tf.initialize_all_variables())
def isFire(*Tinput): XInput = Tinput[0] tf.set_random_seed(777) image_depth = 2 # lrn(2, 2e-05, 0.75, name='norm1') radius = 2 alpha = 2e-05 beta = 0.75 bias = 1.0 # --------------------------------------------------------------------------------------------------- # X: input 32*32*image_depth # Y: output '1' or '0' X = tf.placeholder(tf.float32, [None, 32 * 32 * image_depth]) Y = tf.placeholder(tf.int32, [None, 1]) # 0,1 # 출력 class 개수 = 1(fire), 0(not fire) nb_classes = 2 # one hot & reshape Y_one_hot = tf.one_hot(Y, nb_classes) # print("one_hot", Y_one_hot) Y_one_hot = tf.reshape(Y_one_hot, [-1, nb_classes]) # print("reshape", Y_one_hot) # img 32x32x1 (black/white) X_img = tf.reshape(X, [-1, 32, 32, image_depth]) # --------------------------------------------------------------------------------------------------- # L1 ImgIn shape = (?, 32, 32, image_depth) W1 = tf.Variable(tf.random_normal([3, 3, image_depth, 64], stddev=0.01)) # Conv1 -> (?, 32, 32, 64) L1 = tf.nn.conv2d(X_img, W1, strides=[1, 1, 1, 1], padding='SAME') # Conv2 -> (?, 32, 32, 64) L1 = tf.nn.conv2d(X_img, W1, strides=[1, 1, 1, 1], padding='SAME') L1 = tf.nn.relu(L1) # lrn1 # lrn(2, 2e-05, 0.75, name='norm1') L1 = tf.nn.local_response_normalization(L1, depth_radius=radius, alpha=alpha, beta=beta, bias=bias) # Pool -> (?, 16, 16, 64) L1 = tf.nn.max_pool(L1, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding='SAME') # --------------------------------------------------------------------------------------------------- # L2 ImgIn shape = (?, 16, 16, 64) W2 = tf.Variable(tf.random_normal([3, 3, 64, 128], stddev=0.01)) # Conv1 -> (?, 16, 16, 128) L2 = tf.nn.conv2d(L1, W2, strides=[1, 1, 1, 1], padding='SAME') # Conv2 -> (?, 16, 16, 128) L2 = tf.nn.conv2d(L1, W2, strides=[1, 1, 1, 1], padding='SAME') L2 = tf.nn.relu(L2) # lrn2 # lrn(2, 2e-05, 0.75, name='norm1') L2 = tf.nn.local_response_normalization(L2, depth_radius=radius, alpha=alpha, beta=beta, bias=bias) # Pool -> (?, 8, 8, 128) L2 = tf.nn.max_pool(L2, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding='SAME') # --------------------------------------------------------------------------------------------------- # L3 ImgIn shape = (?, 8, 8, 128) W3 = tf.Variable(tf.random_normal([3, 3, 128, 256], stddev=0.01)) # Conv1 -> (?, 8, 8, 256) L3 = tf.nn.conv2d(L2, W3, strides=[1, 1, 1, 1], padding='SAME') # Conv2 -> (?, 8, 8, 256) L3 = tf.nn.conv2d(L2, W3, strides=[1, 1, 1, 1], padding='SAME') # Conv3 -> (?, 8, 8, 256) L3 = tf.nn.conv2d(L2, W3, strides=[1, 1, 1, 1], padding='SAME') L3 = tf.nn.relu(L3) # Pool -> (?, 4, 4, 256) L3 = tf.nn.max_pool(L3, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding='SAME') # --------------------------------------------------------------------------------------------------- # L4 ImgIn shape = (?, 4, 4, 256) W4 = tf.Variable(tf.random_normal([3, 3, 256, 512], stddev=0.01)) # Conv1 -> (?, 4, 4, 512) L4 = tf.nn.conv2d(L3, W4, strides=[1, 1, 1, 1], padding='SAME') # Conv2 -> (?, 4, 4, 512) L4 = tf.nn.conv2d(L3, W4, strides=[1, 1, 1, 1], padding='SAME') # Conv3 -> (?, 4, 4, 512) L4 = tf.nn.conv2d(L3, W4, strides=[1, 1, 1, 1], padding='SAME') L4 = tf.nn.relu(L4) # Pool -> (?, 2, 2, 512) L4 = tf.nn.max_pool(L4, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding='SAME') # --------------------------------------------------------------------------------------------------- # L5 ImgIn shape = (?, 2, 2, 512) W5 = tf.Variable(tf.random_normal([2, 2, 512, 512], stddev=0.01)) # Conv1 -> (?, 2, 2, 512) L5 = tf.nn.conv2d(L4, W5, strides=[1, 1, 1, 1], padding='SAME') # Conv2 -> (?, 2, 2, 512) L5 = tf.nn.conv2d(L4, W5, strides=[1, 1, 1, 1], padding='SAME') # Conv3 -> (?, 2, 2, 512) L5 = tf.nn.conv2d(L4, W5, strides=[1, 1, 1, 1], padding='SAME') L5 = tf.nn.relu(L5) # Pool -> (?, 1, 1, 512) L5 = tf.nn.max_pool(L5, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding='SAME') # Reshape -> (?, 1 * 1 * 512) - Flatten them for FC L5_flat = tf.reshape(L5, [-1, 1 * 1 * 512]) # --------------------------------------------------------------------------------------------------- # L6 FC 1x1x512 inputs -> 4096 outputs W6 = tf.get_variable("W6", shape=[512 * 1 * 1, 4096], initializer=tf.contrib.layers.xavier_initializer()) b6 = tf.Variable(tf.random_normal([4096])) L6 = tf.nn.relu(tf.matmul(L5_flat, W6) + b6) # --------------------------------------------------------------------------------------------------- # L7 FC 4096 inputs -> 1000 outputs W7 = tf.get_variable("W7", shape=[4096, 1000], initializer=tf.contrib.layers.xavier_initializer()) b7 = tf.Variable(tf.random_normal([1000])) L7 = tf.nn.relu(tf.matmul(L6, W7) + b7) # --------------------------------------------------------------------------------------------------- # L8 FC 1000 inputs -> 1 outputs W8 = tf.get_variable("W8", shape=[1000, nb_classes], initializer=tf.contrib.layers.xavier_initializer()) b8 = tf.Variable(tf.random_normal([nb_classes])) logits = tf.matmul(L7, W8) + b8 # --------------------------------------------------------------------------------------------------- # initialize sess = tf.Session() sess.run(tf.global_variables_initializer()) # --------------------------------------------------------------------------------------------------- # predict saver = tf.train.Saver() saver.restore(sess, os.getcwd() + "/ISFire(new model).ckpt") prediction = tf.argmax(logits, 1) predict_output = sess.run(prediction, feed_dict={X: XInput}) sess.close() return predict_output
def __init__(self, args, d, logdir): super(dynamic_bern_emb_model, self).__init__(args, d, logdir) with tf.name_scope('model'): with tf.name_scope('embeddings'): self.alpha = tf.Variable(self.alpha_init, name='alpha', trainable=self.alpha_trainable) self.rho_t = {} for t in range(-1,self.T): self.rho_t[t] = tf.Variable(self.rho_init + 0.001*tf.random_normal([self.L, self.K])/self.K, name = 'rho_'+str(t)) with tf.name_scope('priors'): global_prior = Normal(loc = 0.0, scale = self.sig) local_prior = Normal(loc = 0.0, scale = self.sig/100.0) self.log_prior = tf.reduce_sum(global_prior.log_prob(self.alpha)) self.log_prior = tf.reduce_sum(global_prior.log_prob(self.rho_t[-1])) for t in range(self.T): self.log_prior += tf.reduce_sum(local_prior.log_prob(self.rho_t[t] - self.rho_t[t-1])) with tf.name_scope('likelihood'): self.placeholders = {} self.y_pos = {} self.y_neg = {} self.ll_pos = 0.0 self.ll_neg = 0.0 for t in range(self.T): # Index Masks p_mask = tf.range(self.cs//2,self.n_minibatch[t] + self.cs//2) rows = tf.tile(tf.expand_dims(tf.range(0, self.cs//2),[0]), [self.n_minibatch[t], 1]) columns = tf.tile(tf.expand_dims(tf.range(0, self.n_minibatch[t]), [1]), [1, int(self.cs/2)]) # columns = tf.cast(columns, tf.float32) # print(type(rows), rows.dtype) # print(type(columns), columns.dtype) ctx_mask = tf.concat([rows+columns, rows+columns +self.cs//2+1], 1) # Data Placeholder self.placeholders[t] = tf.placeholder(tf.int32, shape = (self.n_minibatch[t] + self.cs)) # Taget and Context Indices p_idx = tf.gather(self.placeholders[t], p_mask) ctx_idx = tf.squeeze(tf.gather(self.placeholders[t], ctx_mask)) # Negative samples unigram_logits = tf.tile(tf.expand_dims(tf.log(tf.constant(self.unigram)), [0]), [self.n_minibatch[t], 1]) n_idx = tf.multinomial(unigram_logits, self.ns) # Context vectors ctx_alphas = tf.gather(self.alpha, ctx_idx) p_rho = tf.squeeze(tf.gather(self.rho_t[t], p_idx)) n_rho = tf.gather(self.rho_t[t], n_idx) # Natural parameter ctx_sum = tf.reduce_sum(ctx_alphas,[1]) p_eta = tf.expand_dims(tf.reduce_sum(tf.multiply(p_rho, ctx_sum),-1),1) n_eta = tf.reduce_sum(tf.multiply(n_rho, tf.tile(tf.expand_dims(ctx_sum,1),[1,self.ns,1])),-1) # Conditional likelihood self.y_pos[t] = Bernoulli(logits = p_eta) self.y_neg[t] = Bernoulli(logits = n_eta) self.ll_pos += tf.reduce_sum(self.y_pos[t].log_prob(1.0)) self.ll_neg += tf.reduce_sum(self.y_neg[t].log_prob(0.0)) self.loss = - (self.n_epochs * (self.ll_pos + self.ll_neg) + self.log_prior)
noise_std=0.01) weights_variables = [ tf.Variable(new_weights[0]), tf.Variable(new_weights[1]), tf.Variable(new_weights[2])] biases_variables = [ tf.Variable(new_biases[0]), tf.Variable(new_biases[1]), tf.Variable(new_biases[2])] return weights_variables, biases_variables # Store layers weight & bias weight_variables = [tf.Variable(tf.random_normal([n_input, minimal_n_hidden_1])), tf.Variable(tf.random_normal([minimal_n_hidden_1, minimal_n_hidden_2])), tf.Variable(tf.random_normal([minimal_n_hidden_2, n_classes]))] bias_variables = [tf.Variable(tf.zeros([minimal_n_hidden_1])), tf.Variable(tf.zeros([minimal_n_hidden_2])), tf.Variable(tf.zeros([n_classes]))] # Construct model cost, optimizer, pred = multilayer_perceptron(x, weight_variables, bias_variables) results = [] # Launch the graph with tf.Session() as sess: new_all_variables = set(tf.all_variables())
def init_opt(self): self.build_placeholder() with pt.defaults_scope(phase=pt.Phase.train): # #### autoencoder pretrain format ################################ with tf.variable_scope("g_net"): # real f to Y c, kl_loss = self.sample_encoded_context(self.embeddings) z2 = tf.random_normal([self.batch_size, cfg.Z_DIM]) self.gen_images = self.model.get_generator( tf.concat([c, z2], 1)) # generate f to Y b_zero = tf.zeros([self.batch_size, self.opt.latent_size]) self.latent_z1 = tf.random_normal( [self.batch_size, self.latent_size]) self.fake_embeddings = self.model.get_emb_gen( tf.concat([b_zero, self.latent_z1], 1)) c_, _ = self.sample_encoded_context(self.fake_embeddings) z2_ = tf.random_normal([self.batch_size, cfg.Z_DIM]) self.gen_fake_images = self.model.get_generator( tf.concat([c_, z2_], 1)) # generate Y to f c_zero = tf.zeros([self.batch_size, self.opt.ef_dim]) self.latent_z2 = tf.random_normal([self.batch_size, cfg.Z_DIM]) self.fake_images = self.model.get_generator( tf.concat([c_zero, self.latent_z2], 1)) b_ = self.model.get_CNN(self.fake_images) z1_ = tf.random_normal([self.batch_size, self.latent_size]) self.gen_fake_embeddings = self.model.get_emb_gen( tf.concat([b_, z1_], 1)) # real Y to f b = self.model.get_CNN(self.images) z1 = tf.random_normal([self.batch_size, self.latent_size]) self.gen_embeddings = self.model.get_emb_gen( tf.concat([b, z1], 1)) self.log_vars.append(("hist_c", c)) self.log_vars.append(("hist_c_", c_)) self.log_vars.append(("hist_z", z2)) self.log_vars.append(("hist_z_", z2_)) with tf.variable_scope("pretrains"): _, _, self.fake_words, _ = self.model.get_lstm_decoder_embedding( self.fake_embeddings, self.real_words, self.real_W_norm, feed_previous=True) # ####get discriminator_loss and generator_loss ################### discriminator_loss, generator_loss =\ self.compute_losses(self.images, self.embeddings, self.fake_images, self.fake_embeddings, self.gen_images, self.gen_embeddings, self.gen_fake_images, self.gen_fake_embeddings, self.wrong_images ) # rec_loss = tf.reduce_mean(tf.sqrt(tf.reduce_sum(tf.square(self.latent_z2 - self.rec_latent_z2),1))) MMD_b_loss = self.model.get_MMD_loss(b_, b) generator_loss += kl_loss + MMD_b_loss self.log_vars.append(("g_loss_kl_loss", kl_loss)) self.log_vars.append(("g_MMD_b_loss", MMD_b_loss)) self.log_vars.append(("g_loss", generator_loss)) self.log_vars.append(("d_loss", discriminator_loss)) # #######Total loss for build optimizers########################### self.prepare_trainer(generator_loss, discriminator_loss) # #######define self.g_sum, self.d_sum,....######################## self.define_summaries() with pt.defaults_scope(phase=pt.Phase.test): with tf.variable_scope("g_net", reuse=True): self.sampler_images() self.sampler_embeddings() with tf.variable_scope("pretrains"): _, _, self.gen_fake_words_samples, _ = self.model.get_lstm_decoder_embedding( self.gen_fake_embeddings_samples, self.real_words, self.real_W_norm, feed_previous=True, is_reuse=True) self.visualization(cfg.TRAIN.NUM_COPY) print("success")
import tensorflow x_data = [1, 2, 3] y_data = [1, 2, 3] W = tensorflow.Variable(tensorflow.random_normal([1]), name='weight') X = tensorflow.placeholder(tensorflow.float32) Y = tensorflow.placeholder(tensorflow.float32) hypothesis = X * W
def init_matrix(self, shape): return tf.random_normal(shape, stddev=1.0)
Prob_MRF_val_labels[i][int(val_labels[i][0])-1] = 1. print ('Prob_MRF_val_labels.shape', Prob_MRF_val_labels.shape) print ("======================================") n_hidden_1 = 100 n_hidden_2 = 100 x = tf.placeholder(tf.float32, [None, n_input])#列是220 行不定 pixel_coordinate = tf.placeholder(tf.float32, [None, n_coordinate])#列是2 行不定 y = tf.placeholder(tf.float32, [None, n_classes])#列是16 行不定 keep_prob = tf.placeholder(tf.float32) #dropout (keep probability) #获取矩阵的值需要调用sess.run( )方法。 weights = { #1x5 conv, 1 input, 32 outputs 'wc1': tf.Variable(tf.random_normal([1, 14, 1, 20])), #f_hight, f_width, input_channel, output_channel 'wd1_1': tf.Variable(tf.random_normal([18*20, 100])),#通道800到100 图片大小*通道数 40为卷积后池化结果 'wd1_2': tf.Variable(tf.random_normal([n_coordinate, 256])),#先将2维的坐标信息扩展到256维,再将其降到100维 'wd1_2_1': tf.Variable(tf.random_normal([256, 100])), #1024 inputs, 10 outputs #LSTM层 'out': tf.Variable(tf.random_normal([n_hidden_2, n_classes]))#100,16 } biases = { 'bc1': tf.Variable(tf.random_normal([20])), 'bc2': tf.Variable(tf.random_normal([20])), 'bd1_1': tf.Variable(tf.random_normal([100])), 'bd1_2': tf.Variable(tf.random_normal([256])), 'bd1_2_1': tf.Variable(tf.random_normal([100])),
mnist = input_data.read_data_sets("./mnist/data/",one_hot=True) num_epoch = 100000 batch_size = 64 num_input = 28*28 num_latent_variable = 100 num_hidden = 128 learning_rate = 0.001 X = tf.placeholder(tf.float32, [None, num_input]) z = tf.placeholder(tf.float32, [None,num_latent_variable]) with tf.variable_scope('generator'): G_W1 = tf.Variable(tf.random_normal(shape=[num_latent_variable,num_hidden], stddev=5e-2)) G_b1 = tf.Variable(tf.constant(0.1,shape=[num_hidden])) G_W2 = tf.Variable(tf.random_normal(shape=[num_hidden,num_input], stddev=5e-2)) G_b2 = tf.Variable(tf.constant(0.1,shape=[num_input])) with tf.variable_scope('discriminator'): D_W1 = tf.Variable(tf.random_normal(shape=[num_input,num_hidden], stddev=5e-2)) D_b1 = tf.Variable(tf.constant(0.1,shape=[num_hidden])) D_W2 = tf.Variable(tf.random_normal(shape=[num_hidden,1], stddev=5e-2)) D_b2 = tf.Variable(tf.constant(0.1,shape=[1])) def build_generator(X): hidden_layer = tf.nn.relu((tf.matmul(X, G_W1) + G_b1)) output_layer = tf.matmul(hidden_layer,G_W2) + G_b2
def model(sess, batch_size): # Placeholders for input image, question and output answer image = tf.placeholder("float", [None, 14, 14, 512], name="VGG") ques = tf.placeholder("float", [None, 22, 50], name="ques") ans = tf.placeholder("float", [None, 1000], name="ans") with tf.variable_scope('Image') as scope: img_w = tf.Variable(tf.random_normal([1,1,512,128]), name="weight") img_b = tf.Variable(tf.random_normal([128]), name="bias") img_conv = tf.nn.conv2d(image, img_w, strides=[1,1,1,1], padding='SAME') img_feature = tf.nn.relu(img_conv + img_b, name="activation") variable_summaries(img_w) # Learn semantics of question using LSTM with tf.variable_scope('Question') as scope: ques_w = tf.Variable(tf.random_normal([256, 128]), name="W_qa") ques_b = tf.Variable(tf.random_normal([128]), name="b_A") ques_sem = ques_semantics(ques, ques_w, ques_b) ques_vec = tf.reshape(ques_sem, shape=[-1, 1, 1, 128]) variable_summaries(ques_w) # Get attention probabilities with tf.variable_scope('Attention') as scope: with tf.variable_scope('embedding') as scope: visual_embed = tf.multiply(img_feature, ques_vec) variable_summaries(visual_embed) with tf.variable_scope('probability') as scope: embed_squeeze = tf.reduce_sum(tf.reduce_sum(visual_embed, axis=2), axis=1) attention_prob = tf.nn.softmax(embed_squeeze, name="p_I") # Build reduced Image feature using attention map with tf.variable_scope('ReducedImage') as scope: attention_prob = tf.reshape(attention_prob, shape=[-1,1,1,128]) rimage_feature = tf.multiply(img_feature, attention_prob) variable_summaries(rimage_feature) reduced_image_visualize = tf.reshape(tf.reduce_sum(rimage_feature, axis=3), shape=[-1, 14, 14, 1]) attention_summary = tf.summary.image('weighted-features', reduced_image_visualize) # Adding a convolution layer to reduce dimensions with tf.variable_scope('RImage') as scope: red_img_w = tf.Variable(tf.random_normal([1,1,128,8]), name="weight") red_img_b = tf.Variable(tf.random_normal([8]), name="bias") red_img_conv = tf.nn.conv2d(rimage_feature, red_img_w, strides=[1,1,1,1], padding='SAME') red_img_activ = tf.nn.relu(red_img_conv + red_img_b, name="activation") reduced_image_feature = tf.reshape(red_img_activ, shape=[-1, 1568]) variable_summaries(red_img_w) with tf.variable_scope('Image') as scope: img_w = tf.Variable(tf.random_normal([1,1,128,8]), name="weight") img_b = tf.Variable(tf.random_normal([8]), name="bias") img_conv = tf.nn.conv2d(img_feature, img_w, strides=[1,1,1,1], padding='SAME') img_activ = tf.nn.relu(img_conv + img_b, name="activation") image_feature = tf.reshape(img_activ, shape=[-1, 1568]) variable_summaries(img_w) # Combine all three features to build dense layer with tf.variable_scope('Dense') as scope: with tf.variable_scope('question') as scope: sem_dense_w = tf.Variable(tf.random_normal([128, 1000]), name="q_weight") variable_summaries(sem_dense_w) with tf.variable_scope('image') as scope: img_dense_w = tf.Variable(tf.random_normal([1568, 1000]), name="i_weight") variable_summaries(img_dense_w) with tf.variable_scope('attention') as scope: reduced_img_dense_w = tf.Variable(tf.random_normal([1568, 1000]), name="ri_weight") variable_summaries(reduced_img_dense_w) with tf.variable_scope('bias') as scope: dense_b = tf.Variable(tf.random_normal([1000]), name="ans_bias") variable_summaries(dense_b) dense = tf.matmul(ques_sem, sem_dense_w) + \ tf.matmul(reduced_image_feature, reduced_img_dense_w) + \ tf.matmul(image_feature, img_dense_w) + \ dense_b # Apply softmax on dense layer to get answers cross_entropy = tf.nn.softmax_cross_entropy_with_logits(logits=dense, labels=ans) mean_cross_entropy = tf.reduce_mean(cross_entropy) tf.summary.scalar('mean_cross_entropy', mean_cross_entropy) # Create Optimizer for reducing loss optimizer = tf.train.AdamOptimizer(learning_rate=0.01).minimize(mean_cross_entropy) # Evaluate model correct_prediction = tf.equal(tf.argmax(ans, 1), tf.argmax(dense, 1)) accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32)) tf.summary.scalar('accuracy', accuracy) # Return Model parameters return image, ques, ans, optimizer, mean_cross_entropy, accuracy
def conv_net_classifier(): L1 = 32 # number of convolutions for first layer L2 = 64 # number of convolutions for second layer L3 = 1024 # number of neurons for dense layer learning_date = 1e-4 # learning rate epochs = 1 # number of times we loop through training data batch_size = 10 # number of data per batch train_data, test_data, train_labels, test_labels = load.hmp_hmpii_data() features = train_data.shape[1] print "features:"+str(features) classes = train_labels.shape[1] sess = tf.InteractiveSession() xs = tf.placeholder(tf.float32, [None, features]) ys = tf.placeholder(tf.float32, [None, classes]) keep_prob = tf.placeholder(tf.float32) x_shape = tf.reshape(xs, [-1, 1, features, 1]) # auto encoder num_hidden_1 = 512 num_hidden_2 = 256 num_input = 1024 X = tf.placeholder("float", [None, num_input]) weights = { 'encoder_h1': tf.Variable(tf.random_normal([num_input, num_hidden_1])), 'encoder_h2': tf.Variable(tf.random_normal([num_hidden_1, num_hidden_2])), 'decoder_h1': tf.Variable(tf.random_normal([num_hidden_2, num_hidden_1])), 'decoder_h2': tf.Variable(tf.random_normal([num_hidden_1, num_input])) } biases = { 'encoder_b1': tf.Variable(tf.random_normal([num_hidden_1])), 'encoder_b2': tf.Variable(tf.random_normal([num_hidden_2])), 'decoder_b1': tf.Variable(tf.random_normal([num_hidden_1])), 'decoder_b2': tf.Variable(tf.random_normal([num_input])) } ###############autoencoder######################################################### train_features_ace = np.zeros((len(train_data), num_hidden_1), dtype=float) train_labels_ace = np.zeros(len(train_data), dtype=int) test_labels_ace = np.zeros(len(test_data), dtype=int) # Building the encoder def encoder(x): # Encoder Hidden layer with sigmoid activation #1 layer_1 = tf.nn.sigmoid(tf.add(tf.matmul(x, weights['encoder_h1']), biases['encoder_b1'])) # Encoder Hidden layer with sigmoid activation #2 layer_2 = tf.nn.sigmoid(tf.add(tf.matmul(layer_1, weights['encoder_h2']), biases['encoder_b2'])) return layer_2 # Buildding the decoder def decoder(x): # Decoder Hidden layer with sigmoid activation #1 layer_1 = tf.nn.sigmoid(tf.add(tf.matmul(x, weights['decoder_h1']), biases['decoder_b1'])) # Decoder Hidden layer with sigmoid activation #2 layer_2 = tf.nn.sigmoid(tf.add(tf.matmul(layer_1, weights['decoder_h2']), biases['decoder_b2'])) return layer_2 # Construct model encoder_op = encoder(X) decoder_op = decoder(encoder_op) # Prediction y_pred = decoder_op # Targets (Labels) are the input data. y_true = X # Define loss and optimizer, minimize the squared error loss = tf.reduce_mean(tf.pow(y_true - y_pred, 2)) optimizer = tf.train.RMSPropOptimizer(0.01).minimize(loss) # first conv w_conv1 = weight_variable([5, 5, 1, L1]) b_conv1 = bias_variable([L1]) h_conv1 = tf.nn.relu(conv2d(x_shape, w_conv1) + b_conv1) h_pool1 = max_pool_2x2(h_conv1) # second conv w_conv2 = weight_variable([5, 5, L1, L2]) b_conv2 = bias_variable([L2]) h_conv2 = tf.nn.relu(conv2d(h_pool1, w_conv2) + b_conv2) h_pool2 = max_pool_2x2(h_conv2) tmp_shape = (int)(math.ceil(features/4.0)) print tmp_shape h_pool2_flat = tf.reshape(h_pool2, [-1, 1 * tmp_shape * L2]) # third dense layer,full connected w_fc1 = weight_variable([1 * tmp_shape * L2, L3]) b_fc1 = bias_variable([L3]) h_fc1 = tf.nn.relu(tf.matmul(h_pool2_flat, w_fc1) + b_fc1) h_fc1_drop = tf.nn.dropout(h_fc1, keep_prob) # fourth layer, output w_fc2 = weight_variable([L3, classes]) b_fc2 = bias_variable([classes]) y_conv = tf.nn.softmax(tf.matmul(h_fc1_drop, w_fc2) + b_fc2) cross_entropy = tf.reduce_mean(-tf.reduce_sum(ys * tf.log(y_conv), reduction_indices=[1])) train_step = tf.train.AdamOptimizer(learning_date).minimize(cross_entropy) correct_prediction = tf.equal(tf.argmax(y_conv, 1), tf.argmax(ys, 1)) accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32)) train_features_cnn = np.zeros((len(train_data), L3), dtype=float) train_labels_cnn = np.zeros(len(train_data), dtype=int) test_labels_cnn = np.zeros(len(test_data), dtype=int) train_labels_classifier = np.zeros(len(train_data), dtype=int) test_labels_classifier = np.zeros(len(test_data), dtype=int) converter = np.arange(classes) ### classifier##################################### for i in range(len(train_labels)): train_labels_classifier[i] = np.sum(np.multiply(converter, train_labels[i, :])) for j in range(len(test_labels)): test_labels_classifier[j] = np.sum(np.multiply(converter, test_labels[j, :])) clf = svm.SVC(kernel='linear', C=1, gamma=0.001, random_state=0, probability=True) # clf = RandomForestClassifier(n_estimators=1000, random_state=0) # srhl_rbf = RBFRandomLayer(n_hidden=50, rbf_width=0.1, random_state=0) # clf = GenELMClassifier(hidden_layer=srhl_rbf) clf.fit(train_data, train_labels_classifier) clf_accuracy = clf.score(test_data, test_labels_classifier) print "classifier accuracy = " + str(clf_accuracy) ##################################################### sess.run(tf.global_variables_initializer()) ### cnn start train################################## for epoch in range(epochs): # print 'epoch: ' + str(epoch) for batch in range(len(train_data) // batch_size): offset = (batch * batch_size) % len(train_data) batch_data = train_data[offset:(offset + batch_size)] batch_labels = train_labels[offset:(offset + batch_size)] train_step.run(feed_dict={xs: batch_data, ys: batch_labels, keep_prob: 0.5}) ### cnn test### accuracy = accuracy.eval(feed_dict={xs: test_data, ys: test_labels, keep_prob: 1.0}) print "conv_net accuracy = " + str(accuracy) ### cnn and classifier start train#################### for epoch in range(epochs): for batch in range(len(train_data) // batch_size): offset = (batch * batch_size) % len(train_data) train_batch_data = train_data[offset:(offset + batch_size)] train_batch_labels = train_labels[offset:(offset + batch_size)] features_batch = h_fc1.eval(feed_dict={xs: train_batch_data}) for j in range(batch_size): for k in range(L3): train_features_cnn[batch_size * batch + j, k] = features_batch[j, k] train_labels_cnn[batch_size * batch + j] = np.sum(np.multiply(converter, train_batch_labels[j, :])) test_features_cnn = h_fc1.eval(feed_dict={xs: test_data}) for j in range(len(test_data)): test_labels_cnn[j] = np.sum(np.multiply(converter, test_labels[j, :])) # clf = svm.SVC(kernel='linear', C=1, gamma=0.001, random_state=0, probability=True) # clf = RandomForestClassifier( # n_estimators=1000, random_state=0).fit(train_features_cnn, train_labels_cnn) # srhl_rbf = RBFRandomLayer(n_hidden=50, rbf_width=0.1, random_state=0) # clf = GenELMClassifier(hidden_layer=srhl_rbf) for i in range(len(train_features_cnn)): _, l = sess.run([optimizer, loss], feed_dict={X: train_features_cnn[i].reshape(1,1024)}) # display logs per step if i % 100 == 0 or i == 0: print('Step %i: Loss: %f' % (i, l)) train_features_ace = encoder_op.eval(feed_dict={X: train_features_cnn}) train_labels_ace = train_labels_cnn test_features_ace = encoder_op.eval(feed_dict={X: test_features_cnn}) test_labels_ace = test_labels_cnn ############################################################################### clf.fit(train_features_ace, train_labels_ace) conv_clf_accuracy = clf.score(test_features_ace, test_labels_ace) print "conv_net classifier accuracy = " + str(conv_clf_accuracy) sess.close()
import tensorflow as tf w1 = tf.Variable(tf.random_normal([2, 3], stddev=1, seed=1)) w2 = tf.Variable(tf.random_normal([3, 1], stddev=1, seed=1)) # x = tf.constant([[0.7, 0.9]]); x = tf.placeholder(tf.float32, shape=(3, 2), name='input') a = tf.matmul(x, w1) y = tf.matmul(a, w2) sess = tf.Session() # sess.run(w1.initializer); # sess.run(w2.initializer); init_op = tf.initialize_all_variables() sess.run(init_op) print(sess.run(y, feed_dict={x: [[0.7, 0.9], [0.1, 0.4], [0.5, 0.8]]})) sess.close()
def build_sigma_variable(self, shape, init=-5.): """Returns a sigma variable initialized as N(init, 0.05).""" # Initialize sigma to be very small initially to encourage MAP opt first return tf.Variable(tf.random_normal(shape, init, 0.05))
def lighting(image, std, eigval, eigvec): v = tf.random_normal(shape=[3], stddev=std) * eigval inc = tf.matmul(eigvec, tf.reshape(v, [3, 1])) image = tf.cast( tf.cast(image, tf.float32) + tf.reshape(inc, [3]), image.dtype) return image
n_hidden4 = n_hidden2 n_hidden5 = n_hidden1 n_outputs = n_inputs learning_rate = 0.001 initializer = tf.contrib.layers.variance_scaling_initializer() my_dense_layer = partial(tf.layers.dense, activation=tf.nn.elu, kernel_initializer=initializer) X = tf.placeholder(tf.float32, [None, n_inputs]) hidden1 = my_dense_layer(X, n_hidden1) hidden2 = my_dense_layer(hidden1, n_hidden2) hidden3_mean = my_dense_layer(hidden2, n_hidden3, activation=None) hidden3_gamma = my_dense_layer(hidden2, n_hidden3, activation=None) noise = tf.random_normal(tf.shape(hidden3_gamma), dtype=tf.float32) hidden3 = hidden3_mean + tf.exp(0.5 * hidden3_gamma) * noise hidden4 = my_dense_layer(hidden3, n_hidden4) hidden5 = my_dense_layer(hidden4, n_hidden5) logits = my_dense_layer(hidden5, n_outputs, activation=None) outputs = tf.sigmoid(logits) xentropy = tf.nn.sigmoid_cross_entropy_with_logits(labels=X, logits=logits) reconstruction_loss = tf.reduce_sum(xentropy) latent_loss = 0.5 * tf.reduce_sum( tf.exp(hidden3_gamma) + tf.square(hidden3_mean) - 1 - hidden3_gamma) loss = reconstruction_loss + latent_loss optimizer = tf.train.AdamOptimizer(learning_rate=learning_rate) training_op = optimizer.minimize(loss)
def build_model(self, activation_fn=tf.nn.relu): """Defines the actual NN model with fully connected layers. Args: activation_fn: Activation function for the neural network. The loss is computed for partial feedback settings (bandits), so only the observed outcome is backpropagated (see weighted loss). Selects the optimizer and, finally, it also initializes the graph. """ print('Initializing model {}.'.format(self.name)) # Build terms for the noise sigma estimation for each action. noise_sigma_mu = (self.build_mu_variable([1, self.n_out]) + self.sigma_transform.inverse(self.hparams.noise_sigma)) noise_sigma_sigma = self.sigma_transform.forward( self.build_sigma_variable([1, self.n_out])) pre_noise_sigma = noise_sigma_mu + tf.random_normal( [1, self.n_out]) * noise_sigma_sigma self.noise_sigma = self.sigma_transform.forward(pre_noise_sigma) # Build network input_x = self.x n_in = self.n_in self.total_layers = len(self.layers) + 1 if self.layers[0] == 0: self.total_layers = 1 for l_number, n_nodes in enumerate(self.layers): if n_nodes > 0: h = self.build_layer(input_x, [n_in, n_nodes], l_number) input_x = h n_in = n_nodes self.num_w += n_in * n_nodes self.num_b += n_nodes self.y_pred = self.build_layer(input_x, [n_in, self.n_out], self.total_layers - 1, activation_fn=lambda x: x) # Compute energy function based on sampled nn's log_coeff = self.data_size / (self.batch_size * self.alpha) log_ratio = log_coeff * self.log_alpha_likelihood_ratio(activation_fn) logzprior = self.log_z_prior() logzq = self.log_z_q() energy = logzprior - logzq - log_ratio self.loss = energy self.global_step = tf.train.get_or_create_global_step() self.train_op = tf.train.AdamOptimizer(self.hparams.initial_lr).minimize( self.loss, global_step=self.global_step) # Useful for debugging sq_loss = tf.squared_difference(self.y_pred, self.y) weighted_sq_loss = self.weights * sq_loss self.cost = tf.reduce_sum(weighted_sq_loss) / self.batch_size # Create tensorboard metrics self.create_summaries() self.summary_writer = tf.summary.FileWriter('{}/graph_{}'.format( FLAGS.logdir, self.name), self.sess.graph)
mnist = input_data.read_data_sets('MNIST_data/', one_hot=True) # hyperparanmeters lr = 0.001 training_iters = 100000 batch_size = 128 n_inputs = 28 n_step = 28 n_hidden_units = 128 n_classes = 10 x = tf.placeholder(tf.float32, [None, n_step, n_inputs]) y = tf.placeholder(tf.float32, [None, n_classes]) weight = { 'in': tf.Variable(tf.random_normal([n_inputs, n_hidden_units])), 'out': tf.Variable(tf.random_normal([n_hidden_units, n_classes])) } biases = { 'in': tf.Variable(tf.constant(0.1, shape=[n_hidden_units])), 'out': tf.Variable(tf.constant(0.1, shape=[n_classes])) } def RNN(X, weight, biases): X = tf.reshape(X, [-1, n_inputs]) X_in = tf.matmul(X, weight['in']) + biases['in'] X_in = tf.reshape(X_in, [-1, n_step, n_hidden_units]) lstm_cell = tf.contrib.rnn.BasicLSTMCell(n_hidden_units, forget_bias=1.0, state_is_tuple=True)
def build_mu_variable(self, shape): """Returns a mean variable initialized as N(0, 0.05).""" return tf.Variable(tf.random_normal(shape, 0.0, 0.05))
training_iters = 100000 batch_size = 128 display_step = 10 # Network Parameters n_input = 28 # MNIST data input (img shape: 28*28) n_steps = 28 # timesteps n_hidden = 128 # hidden layer num of features n_classes = 10 # MNIST total classes (0-9 digits) # tf Graph input x = tf.placeholder("float", [None, n_steps, n_input]) y = tf.placeholder("float", [None, n_classes]) # Define weights weights = {'out': tf.Variable(tf.random_normal([n_hidden, n_classes]))} biases = {'out': tf.Variable(tf.random_normal([n_classes]))} def RNN(x, weights, biases): # Prepare data shape to match `rnn` function requirements # Current data input shape: (batch_size, n_steps, n_input) # Required shape: 'n_steps' tensors list of shape (batch_size, n_input) # Unstack to get a list of 'n_steps' tensors of shape (batch_size, n_input) x = tf.unstack(x, n_steps, 1) # Define a lstm cell with tensorflow lstm_cell = rnn.BasicLSTMCell(n_hidden, forget_bias=1.0)
testing = mnist.test.images testlabel = mnist.test.labels print("MNIST loaded") print(trainimg.shape) print(trainlabel.shape) print(testing.shape) print(testlabel.shape) # n_hidden_1 = 256 # n_hidden_2 = 128 n_input = 784 n_output = 10 weights = { 'wc1': tf.Variable(tf.random_normal([3, 3, 1, 64], stddev=0.1)), 'wc2': tf.Variable(tf.random_normal([3, 3, 64, 128], stddev=0.1)), 'wd1': tf.Variable(tf.random_normal([7 * 7 * 128, 1024], stddev=0.1)), 'wd2': tf.Variable(tf.random_normal([1024, n_output], stddev=0.1)) } biases = { 'bc1': tf.Variable(tf.random_normal([64], stddev=0.1)), 'bc2': tf.Variable(tf.random_normal([128], stddev=0.1)), 'bd1': tf.Variable(tf.random_normal([1024], stddev=0.1)), 'bd2': tf.Variable(tf.random_normal([n_output], stddev=0.1)) } def conv_basic(_input, _w, _b, _keepratio): _input_r = tf.reshape(_input, shape=[-1, 28, 28, 1]) _conv1 = tf.nn.conv2d(_input_r,
# X_data.shape = (101, 16) => 101 samples, 16 features # y_data.shape = (101, 1) => 101 samples, 1 label print("Shape of X data: ", X_data.shape) print("Shape of y data: ", y_data.shape) nb_classes = 7 # 0 ~ 6 X = tf.placeholder(tf.float32, [None, 16]) y = tf.placeholder(tf.int32, [None, 1]) # 0 ~ 6 target = tf.one_hot(y, nb_classes) # one hot target = tf.reshape(target, [-1, nb_classes]) target = tf.cast(target, tf.float32) W = tf.Variable(tf.random_normal([16, nb_classes]), name='weight') b = tf.Variable(tf.random_normal([nb_classes]), name='bias') def sigma(x): # sigmoid function # σ(x) = 1 / (1 + exp(-x)) return 1. / (1. + tf.exp(-x)) def sigma_prime(x): # derivative of the sigmoid function # σ'(x) = σ(x) * (1 - σ(x)) return sigma(x) * (1. - sigma(x))
# clear the file for next run open('scratch1', 'w').close() # open file to write in order to store results for each run with open('scratch1', 'w') as f: # the for loop iterates through the parameter arrays and runs logistic regression on each set for i in range(0, 1): # the parameters are defined by iterating through the array of numbers learn_rate = lr[i] batch_siz = bs[i] epoch = ne[i] # this creates the placeholders for features # each one of this pictures are held in an array of 1x784 known as a tensor X = tf.placeholder(tf.float32, [batch_siz, 784]) Y = tf.placeholder(tf.float32, [batch_siz, 10]) # Creates and initializes the weights and the bias's for the logistic regression w = tf.Variable(tf.random_normal(shape=[784, 10], stddev=0.01), name="weights") b = tf.Variable(tf.zeros([1, 10]), name="bias") # predicts the model logits = tf.matmul(X, w) + b # defines the loss function using softmax cross entropy entropy = tf.nn.softmax_cross_entropy_with_logits(logits=logits, labels=Y) loss = tf.reduce_mean(entropy) # computes mean cross entropy # Trains the model optimizer = tf.train.GradientDescentOptimizer( learning_rate=learn_rate).minimize(loss) init = tf.global_variables_initializer() with tf.Session() as sess: sess.run(init)
def simple_policy_model(t_id, state): weights = tf.Variable(tf.random_normal((1,3)), name="Policy_weights") #constants = tf.constant([[1, 1, 1]], dtype=tf.float32) + 0 * weights return tf.tile(ks.activations.softmax(weights, axis=1), [tf.shape(state)[0], 1])
FILE_NAME = '/home/folaraz/PycharmProjects/conc_proj/data/concrete.csv' LAMBDA = 0.01 LEARNING_RATE = 0.01 x_train, y_train = utils.train x_test, y_test = utils.test with tf.name_scope('input'): # training data x = tf.placeholder("float", name="components") y = tf.placeholder("float", name="water") with tf.name_scope('weights'): w1 = tf.get_variable(initializer=tf.random_normal([5, 5]), name="W1") w2 = tf.get_variable(initializer=tf.random_normal([5, 2]), name="W2") w3 = tf.get_variable(initializer=tf.random_normal([2, 1]), name="W3") with tf.name_scope('biases'): # biases (we separate them from the weights because it is easier to do that when using TensorFlow) b1 = tf.get_variable(initializer=tf.random_normal([1, 5]), name="B1") b2 = tf.get_variable(initializer=tf.random_normal([1, 2]), name="B2") b3 = tf.get_variable(initializer=tf.random_normal([1, 1]), name="B3") with tf.name_scope('layer_1'): # three hidden layer layer_1 = tf.nn.tanh(tf.add(tf.matmul(x, w1), b1)) with tf.name_scope('layer_2'): layer_2 = tf.nn.tanh(tf.add(tf.matmul(layer_1, w2), b2))
def __call__(self, input_layer, name: str, kernel_initializer=None, activation=None, is_training=None): """ returns a NoisyNet dense layer :param input_layer: previous layer :param name: layer name :param kernel_initializer: initializer for kernels. Default is to use Gaussian noise that preserves stddev. :param activation: the activation function :return: dense layer """ #TODO: noise sampling should be externally controlled. DQN is fine with sampling noise for every # forward (either act or train, both for online and target networks). # A3C, on the other hand, should sample noise only when policy changes (i.e. after every t_max steps) def _f(values): return tf.sqrt(tf.abs(values)) * tf.sign(values) def _factorized_noise(inputs, outputs): # TODO: use factorized noise only for compute intensive algos (e.g. DQN). # lighter algos (e.g. DQN) should not use it noise1 = _f(tf.random_normal((inputs, 1))) noise2 = _f(tf.random_normal((1, outputs))) return tf.matmul(noise1, noise2) num_inputs = input_layer.get_shape()[-1].value num_outputs = self.units stddev = 1 / math.sqrt(num_inputs) activation = activation if activation is not None else (lambda x: x) if kernel_initializer is None: kernel_mean_initializer = tf.random_uniform_initializer( -stddev, stddev) kernel_stddev_initializer = tf.random_uniform_initializer( -stddev * self.sigma0, stddev * self.sigma0) else: kernel_mean_initializer = kernel_stddev_initializer = kernel_initializer with tf.variable_scope(None, default_name=name): weight_mean = tf.get_variable('weight_mean', shape=(num_inputs, num_outputs), initializer=kernel_mean_initializer) bias_mean = tf.get_variable('bias_mean', shape=(num_outputs, ), initializer=tf.zeros_initializer()) weight_stddev = tf.get_variable( 'weight_stddev', shape=(num_inputs, num_outputs), initializer=kernel_stddev_initializer) bias_stddev = tf.get_variable( 'bias_stddev', shape=(num_outputs, ), initializer=kernel_stddev_initializer) bias_noise = _f(tf.random_normal((num_outputs, ))) weight_noise = _factorized_noise(num_inputs, num_outputs) bias = bias_mean + bias_stddev * bias_noise weight = weight_mean + weight_stddev * weight_noise return activation(tf.matmul(input_layer, weight) + bias)
def simple_value_model(t_id, action, x): weights = tf.Variable(tf.random_normal((1, 3)), name="Value_weights") #constants = tf.constant([[1,0,0]], dtype=tf.float32) + 0*weights return tf.reduce_sum(weights*action, keep_dims=True ,axis=1)
n_nodes_hy4 = 100 n_classes = 2 #fit with one-hot format # number = 1 [ 0,1,0,0,0,0,0,0,0,0,0,0] batch_size = 3 #run time , piece by piece #setting format #x = tf.placeholder('float',[None, 25],name="my_x") x = tf.placeholder('float', [None, 25], name="my_x") y = tf.placeholder('float', name="my_y") #Setting format for layer's w,b #下一行中[x,y]-> x表示layer輸入node數, y表示layer輸出node數 hy_1_layer = { 'weights': tf.Variable(tf.random_normal([25, n_nodes_hy1])), 'biases': tf.Variable(tf.random_normal([n_nodes_hy1])) } hy_2_layer = { 'weights': tf.Variable(tf.random_normal([n_nodes_hy1, n_nodes_hy2])), 'biases': tf.Variable(tf.random_normal([n_nodes_hy2])) } hy_3_layer = { 'weights': tf.Variable(tf.random_normal([n_nodes_hy2, n_nodes_hy3])), 'biases': tf.Variable(tf.random_normal([n_nodes_hy3])) } hy_4_layer = { 'weights': tf.Variable(tf.random_normal([n_nodes_hy3, n_nodes_hy4])), 'biases': tf.Variable(tf.random_normal([n_nodes_hy4])) } output_layer = {
def _factorized_noise(inputs, outputs): # TODO: use factorized noise only for compute intensive algos (e.g. DQN). # lighter algos (e.g. DQN) should not use it noise1 = _f(tf.random_normal((inputs, 1))) noise2 = _f(tf.random_normal((1, outputs))) return tf.matmul(noise1, noise2)
import tensorflow as tf # Parameters learning_rate = 0.001 training_epochs = 15 batch_size = 100 display_step = 1 n_input = 784 # MNIST data input (img shape: 28*28) n_classes = 10 # MNIST total classes (0-9 digits) n_hidden_layer = 256 # layer number of features # Store layers weight & bias weights = { 'hidden_layer': tf.Variable(tf.random_normal([n_input, n_hidden_layer])), 'out': tf.Variable(tf.random_normal([n_hidden_layer, n_classes])) } biases = { 'hidden_layer': tf.Variable(tf.random_normal([n_hidden_layer])), 'out': tf.Variable(tf.random_normal([n_classes])) } # tf Graph input x = tf.placeholder("float", [None, 28, 28, 1]) y = tf.placeholder("float", [None, n_classes]) x_flat = tf.reshape(x, [-1, n_input]) # Hidden layer with RELU activation layer_1 = tf.add(tf.matmul(x_flat, weights['hidden_layer']),
# Get lstm cell output outputs, states = tf.nn.dynamic_rnn(cell, embedded_input, dtype=tf.float32, sequence_length=step) last_output = states[num_layers - 1].h t_outputs = tf.transpose(outputs, [1, 0, 2]) t_outputs = tf.split(t_outputs, seq_length, 0) t_target = tf.transpose(target, [1, 0]) t_target = tf.split(t_target, seq_length, 0) total_cost = 0 l_w = tf.Variable(tf.random_normal([rnn_size, voca_size])) l_b = tf.Variable(tf.zeros([voca_size]) + 0.1) for p, t in zip(t_outputs, t_target): p = tf.reshape(p, [-1, rnn_size]) t = tf.reshape(t, [-1]) logit = tf.matmul(p, l_w) + l_b cost = tf.nn.sparse_softmax_cross_entropy_with_logits(logits=logit, labels=t) cost = tf.reduce_mean(cost) total_cost += cost total_cost = total_cost / seq_length