本文整理汇总了Python中tensorflow.random_normal函数的典型用法代码示例。如果您正苦于以下问题:Python random_normal函数的具体用法?Python random_normal怎么用?Python random_normal使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了random_normal函数的20个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的Python代码示例。
示例1: _init_layers
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()
开发者ID:bentzinir,项目名称:Buffe,代码行数:26,代码来源:transition_sep.py
示例2: testResizeTensorsToRangeWithSimilarMinMaxSizes
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])
开发者ID:Exscotticus,项目名称:models,代码行数:28,代码来源:preprocess_utils_test.py
示例3: __init__
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
开发者ID:zhaocq-nlp,项目名称:NeuralDocModel,代码行数:31,代码来源:NVDM.py
示例4: __init__
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))
开发者ID:ilikesongdandan,项目名称:Introduction-to-Programming-Using-Python,代码行数:34,代码来源:model.py
示例5: _add_gtboxes_as_first_stage_proposals
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
开发者ID:mbossX,项目名称:RRPN_FPN_Tensorflow,代码行数:27,代码来源:build_fast_rcnn.py
示例6: __init__
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()
开发者ID:yaoyaowd,项目名称:tensorflow_demo,代码行数:25,代码来源:nn_knn.py
示例7: init_weights
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
开发者ID:chriscremer,项目名称:Other_Code,代码行数:25,代码来源:BNN.py
示例8: build_model
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)
开发者ID:hetangse,项目名称:encdec,代码行数:30,代码来源:model.py
示例9: __init__
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())
开发者ID:kvfrans,项目名称:rl-qlearn,代码行数:28,代码来源:q_basic.py
示例10: _operator_and_mat_and_feed_dict
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
开发者ID:Hwhitetooth,项目名称:tensorflow,代码行数:25,代码来源:linear_operator_diag_test.py
示例11: create_dual_q_network
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
开发者ID:codealphago,项目名称:melee-ai,代码行数:28,代码来源:dqn_atari.py
示例12: fit
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_})
开发者ID:paris007,项目名称:cs224u,代码行数:60,代码来源:shallow_neural_networks.py
示例13: neural_net_init
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 :])]
开发者ID:fenss,项目名称:tat_algorithm,代码行数:7,代码来源:tat_nn_entropy_tf.py
示例14: _build_output
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
开发者ID:clinicalml,项目名称:cfrnet,代码行数:34,代码来源:cfr_net.py
示例15: __init__
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)
开发者ID:mohakbhardwaj,项目名称:reinforcement-learning,代码行数:33,代码来源:cartpole-policy-gradient.py
示例16: _testConfMatrixOnTensors
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)
开发者ID:821760408-sp,项目名称:tensorflow,代码行数:32,代码来源:confusion_matrix_ops_test.py
示例17: test_backward_grads_with_nativepy
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)
开发者ID:Ajaycs99,项目名称:tensorflow,代码行数:33,代码来源:blocks_test.py
示例18: fully_connected_neural_network
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
开发者ID:ykevin,项目名称:autorace2017-team-loading,代码行数:25,代码来源:signal_sign_detection.py
示例19: add_layer
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
开发者ID:gswyhq,项目名称:hello-world,代码行数:35,代码来源:用Tensorflow搭建神经网络.py
示例20: bidirectional_lstm_inference
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
开发者ID:tobegit3hub,项目名称:deep_recommend_system,代码行数:31,代码来源:model.py
注:本文中的tensorflow.random_normal函数示例由纯净天空整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。 |
请发表评论