def read_cifar10(filename_queue): class CIFAR10Record(object): pass result = CIFAR10Record() # 输入标准化 label_bytes = 1 result.height = 32 result.width = 32 result.depth = 3 image_bytes = result.height * result.width * result.depth record_bytes = label_bytes + image_bytes reader = tf.FixedLengthRecordReader(record_bytes=record_bytes) result.key, value = reader.read(filename_queue) # record_bytes = tf.decode_raw(value, tf.uint8) # 把标签从int8转到int32 result.label = tf.cast(tf.slice(record_bytes, [0], [label_bytes]), tf.int32) # reshape图片为长宽高的形式[depth,height,width] depth_major = tf.reshape( tf.slice(record_bytes, [label_bytes], [image_bytes]), [result.depth, result.height, result.width]) # 转化为[height,width,depth] result.uint8image = tf.transpose(depth_major, [1, 2, 0]) return result
def model_fn(features, labels, mode): # 先构建神经网络 logits = neural_network(features) # 预测结果 pred_calsses = tf.argmax(logits, axis=1) pred_probas = tf.nn.softmax(logits) # 如果是 预测模式,早早的返回 if mode == tf.estimator.ModeKeys.PREDICT: return tf.estimator.EstimatorSpec(mode, predictions=pred_calsses) # 定义损失函数和优化函数 loss_op = tf.reduce_mean( tf.nn.sparse_softmax_cross_entropy_with_logits(logits=logits, labels=tf.cast( labels, dtype=tf.int32))) optimizer = tf.train.GradientDescentOptimizer(learning_rate) train_op = optimizer.minimize(loss_op, global_step=tf.train.get_global_step()) # 验证模型的acc acc_op = tf.metrics.accuracy(labels=labels, predictions=pred_calsses) # TF Estimators 需要返回一个EstimatorSpec,从而确定是(训练,预测)哪个操作 estim_specs = tf.estimator.EstimatorSpec( mode=mode, predictions=pred_calsses, loss=loss_op, train_op=train_op, eval_metric_ops={'accuracy': acc_op}) return estim_specs
def inputs(eval_data, data_dir, batch_size): if not eval_data: filenames = [ os.path.join(data_dir, 'data_batch_%d.bin' % i) for i in range(1, 6) ] num_examples_per_epoch = NUM_EXAMPLES_PER_FOR_TRAIN else: filenames = [os.path.join(data_dir, "test_batch.bin")] num_examples_per_epoch = NUM_EXAMPLES_PER_EPOCH_FOR_EVAL for f in filenames: if not gfile.Exists(f): raise ValueError('failed to find file:' + f) filename_queue = tf.train.string_input_producer(filenames) read_input = read_cifar10(filename_queue) reshaped_image = tf.cast(read_input.uint8image, tf.float32) height = IMAGE_SIZE width = IMAGE_SIZE resized_image = tf.image.resize_image_with_crop_or_pad( reshaped_image, width, height) float_image = tf.image.per_image_standardization(reshaped_image) min_fraction_of_examples_in_queue = 0.4 min_queue_examples = int(num_examples_per_epoch * min_fraction_of_examples_in_queue) return _generate_image_and_label_batch(float_image, read_input.label, min_queue_examples, batch_size)
def forward(self,examples,labels): """建立前向传播图""" opts = self._options # 声明所有需要的变量 # embeddings :[vocab-size,emb_size] init_width = 0.5 / opts.emb_dim emb = tf.Variable( tf.random_uniform([opts.vocab_size,opts.emb_dim], -init_width,init_width),name = "emb") self._emb = emb # softmax_weights:[vocab_size,emb_dim] sm_w_t = tf.Variable( tf.zeros([opts.vocab_size,opts.emb_dim]),name="sm_w_t") # softmax bias:[emd_dim] sm_b = tf.Variable( tf.zeros([opts.vocab_size]),name="sm_b") # global step:scalar self.global_step = tf.Variable(0,name="global_step") # 候选采样计算nce loss的节点 labels_matrix = tf.reshape( tf.cast(labels,dtype=tf.int64),[opts.batch_size,1]) # 负采样 sampled_ids, _,_ = (tf.nn.fixed_unigram_candidate_sampler( true_classes=labels_matrix, num_true=1, num_sampled=opts.num_samples, unique=True, range_max=opts.vocab_size, distortion=0.75, unigrams=opts.vocab_counts.tolist())) # 样本的嵌入:[batch_size,emb_dim] example_emb = tf.nn.embedding_lookup(emb,examples) # 标签的权重w:[batch_size,emb_dim] true_w = tf.nn.embedding_lookup(sm_w_t,labels) # 标签的偏差b :[batch_size,1] true_b = tf.nn.embedding_lookup(sm_b,labels) # 采样样本的ids的权重(Weights for sampled ids):[num_sampled,emb_dim] sampled_w = tf.nn.embedding_lookup(sm_w_t, sampled_ids) # 采样样本的 bias :[num_sampled,1] sampled_b = tf.nn.embedding_lookup(sm_b,sampled_ids) # True logits:[batch_size,1] true_logits = tf.reduce_sum(tf.multiply(example_emb,true_w),1) + true_b # 采样样本预测值 sampled logits:[batch_size,num_sampled] sampled_b_vec = tf.reshape(sampled_b,[opts.num_samples]) sampled_logits = tf.matmul(example_emb, sampled_w, transpose_b=True) + sampled_b_vec return true_logits,sampled_logits
def distorted_inputs(data_dir, batch_size): """ :param data_dir: path to the cifar-10 data dict :param batch_size: number of image per batch :return: images: 4D tensor [batch_size,IMAGE_SIZE,IMAGE_SIZE,3] labels: 1D tensor [batch_size] size """ filenames = [ os.path.join(data_dir, 'data_batch_%d.bin' % i) for i in range(1, 6) ] for f in filenames: if not gfile.Exists(f): raise ValueError('Failed to find file:' + f) # 产生要读取的文件名 filename_queue = tf.train.string_input_producer(filenames) # read examples from files in the filename queue read_input = read_cifar10(filename_queue) reshaped_image = tf.cast(read_input.uint8image, tf.float32) height = IMAGE_SIZE width = IMAGE_SIZE # 对图像进行随机变形,产生训练的数据 # 随机裁剪 distorted_image = tf.image.random_crop(reshaped_image, [height, width, 3]) # 随机翻转 distorted_image = tf.image.random_flip_left_right(distorted_image) distorted_image = tf.image.random_brightness(distorted_image, max_delta=63) distorted_image = tf.image.random_contrast(distorted_image, lower=0.2, upper=1.8) float_image = tf.image.per_image_standardization(distorted_image) min_fraction_of_examples_in_queue = 0.4 min_queue_examples = int(NUM_EXAMPLES_PER_FOR_TRAIN * min_fraction_of_examples_in_queue) print('Filling queue with %d CIFAR images before starting to train. ' 'This will take a few minutes.' % min_queue_examples) return _generate_image_and_label_batch(float_image, read_input.label, min_queue_examples, batch_size)
def optimize(self,loss): """为优化损失函数构造图""" # 线性学习率衰减 opts = self._options words_to_train = float(opts.word_per_epoch * opts.epoch_to_train) lr = opts.learning_rate * tf.maximum( 0.0001,1.0-tf.cast(self._words,tf.float32)/words_to_train ) self._lr = lr optimizer = tf.train.GradientDescentOptimizer(lr) train = optimizer.minimize( loss,global_step=self.global_step, gate_gradients=optimizer.GATE_NONE ) self._train = train
def evaluation(logits, labels): """Evaluate the quality of the logits at predicting the label. Args: logits: Logits tensor, float - [batch_size, NUM_CLASSES]. labels: Labels tensor, int32 - [batch_size], with values in the range [0, NUM_CLASSES). Returns: A scalar int32 tensor with the number of examples (out of batch_size) that were predicted correctly. """ # For a classifier model, we can use the in_top_k Op. # It returns a bool tensor with shape [batch_size] that is true for # the examples where the label is in the top k (here k=1) # of all logits for that example. correct = tf.nn.in_top_k(logits, labels, 1) # Return the number of true entries. return tf.reduce_sum(tf.cast(correct, tf.int32))
def model_fn(features, labels, mode): logits_train = conv_network(features, num_classes, dropout, reuse=False, is_training=True) logits_test = conv_network(features, num_classes, dropout, reuse=True, is_training=False) pred_classes = tf.argmax(logits_test, axis=1) pred_probs = tf.nn.softmax(logits_test) if mode == tf.estimator.ModeKeys.PREDICT: return tf.estimator.EstimatorSpec(mode, predictions=pred_classes) loss_op = tf.reduce_mean( tf.nn.sparse_softmax_cross_entropy_with_logits(logits=logits_train, labels=tf.cast( labels, dtype=tf.int32))) optimizer = tf.train.AdamOptimizer(learning_rate) train_op = optimizer.minimize(loss_op, global_step=tf.train.get_global_step()) # 验证模型的acc acc_op = tf.metrics.accuracy(labels=labels, predictions=pred_classes) estim_spec = tf.estimator.EstimatorSpec( mode=mode, predictions=pred_classes, loss=loss_op, train_op=train_op, eval_metric_ops={'accuracy': acc_op}) return estim_spec
W = tf.Variable(tf.zeros([784,10])) b = tf.Variable(tf.zeros([10])) sess.run(tf.initialize_all_variables()) y = tf.nn.softmax(tf.matmul(x,W)+b) cross_entropy = -tf.reduce_sum(y_*tf.log(y)) train_step = tf.train.GradientDescentOptimizer(0.01).minimize(cross_entropy) for i in range(1000): batch = mnist.train.next_batch(50) train_step.run(feed_dict={x:batch[0],y_:batch[1]}) correct_prediction = tf.equal(tf.argmax(y,1),tf.argmax(y_,1)) accuracy = tf.reduce_mean(tf.cast(correct_prediction,"float")) print(accuracy.eval(feed_dict={x: mnist.test.images, y_: mnist.test.labels}))
# 构造模型并将所有操作封装到scope中,方便tensorboard可视化。 with tf.name_scope('Model'): pred = tf.nn.softmax(tf.matmul(x, W) + b) with tf.name_scope('Loss'): cost = tf.reduce_mean(-tf.reduce_sum(y * tf.log(pred), reduction_indices=1)) with tf.name_scope('SGD'): optimizer = tf.train.GradientDescentOptimizer(learning_rate).minimize(cost) with tf.name_scope('Accuracy'): acc = tf.equal(tf.argmax(pred, 1), tf.argmax(y, 1)) acc = tf.reduce_mean(tf.cast(acc, tf.float32)) init = tf.global_variables_initializer() tf.summary.scalar("loss", cost) tf.summary.scalar("accuracy", acc) merged_summary_op = tf.summary.merge_all() with tf.Session() as sess: sess.run(init) summary_writer = tf.summary.FileWriter(logs_path, graph=tf.get_default_graph()) for epoch in range(train_epochs):
optimizer = tf.train.AdamOptimizer(learning_rate) # 计算梯度 grad = tfe.implicit_gradients(loss_fn) # 开始训练 average_loss = 0. average_acc = 0. for step in range(num_steps): d = dataset_iter.next() # Images x_batch = d[0] y_batch = tf.cast(d[1], dtype=tf.int64) # 计算整个batch的loss batch_loss = loss_fn(neural_network, x_batch, y_batch) average_loss += batch_loss # 计算整个batch的accuracy batch_accuracy = accuracy_fn(neural_network, x_batch, y_batch) average_acc += batch_accuracy if step == 0: # 打印优化前的初始的cost print("Initial loss= {:.9f}".format(average_loss)) optimizer.apply_gradients(grad(neural_network, x_batch, y_batch)) # 打印细节 if (step + 1) % display_step == 0 or step == 0: if step > 0: average_loss /= display_step
a_cyclic = (6.28 * a / 20.0).reshape(list(a.shape) + [1]) img = np.concatenate([ 10 + 20 * np.cos(a_cyclic), 30 + 50 * np.sin(a_cyclic), 155 - 80 * np.cos(a_cyclic) ], 2) img[a == a.max()] = 0 a = img a = np.uint8(np.clip(a, 0, 255)) img1 = PIL.Image.fromarray(a) plt.imsave("image_tf.png", img1) plt.show() sess = tf.InteractiveSession() Y, X = np.mgrid[-1.3:1.3:0.005, -2:1:0.005] Z = X + 1j * Y xs = tf.constant(Z.astype("complex64")) zs = tf.Variable(xs) ns = tf.Variable(tf.zeros_like(xs, "float32")) tf.initialize_all_variables().run() zs_ = zs * zs + xs not_disverged = tf.abs(zs_) < 4 step = tf.group(zs.assign(zs_), ns.assign_add(tf.cast(not_disverged, "float32"))) for i in range(200): step.run() DisplayFractal(ns.eval())
def evaluation(logits, labels): correct = tf.nn.in_top_k(predictions=logits, targets=labels, k=1) return tf.reduce_sum(input_tensor=tf.cast(correct, tf.int32))
def loss(logits, labels): labels = tf.cast(labels, dtype=tf.int64) return tf.compat.v1.losses.sparse_softmax_cross_entropy(labels=labels, logits=logits)
dropout, reuse=True, is_training=False) # 定义loss和opts,带上logits_train 以使dropout生效 loss_op = tf.reduce_mean( tf.nn.softmax_cross_entropy_with_logits(logits=logits_train, labels=_y)) optimizer = tf.train.AdamOptimizer(learning_rate) grads = optimizer.compute_gradients(loss_op) # 只用其中一个gpu计算acc if i == 0: # Evaluate model (with test logits, for dropout to be disabled) correct_pred = tf.equal(tf.argmax(logits_test, 1), tf.argmax(_y, 1)) accuracy = tf.reduce_mean(tf.cast(correct_pred, tf.float32)) reuse_vars = True tower_grads.append(grads) tower_grads = average_gradients(tower_grads) train_op = optimizer.apply_gradients(tower_grads) init = tf.global_variables_initializer() with tf.Session() as sess: sess.run(init) for step in range(1, num_steps + 1): batch_x, batch_y = mnist.train.next_batch(batch_size * num_gpus) ts = time.time() sess.run(train_op, feed_dict={X: batch_x, Y: batch_y}) te = time.time() - ts
# training for i in range(1, num_steps + 1): _, d, idx = sess.run([train_op, avg_distance, cluster_idx], feed_dict={X: full_data_x}) if i % 10 == 0 or i == 1: print("Step %i, Avg Distance: %f" % (i, d)) # 为质心分配标签 # 使用每次训练的标签,汇总每个质心的所有标签总数 counts = np.zeros(shape=(k, num_classes)) for i in range(len(idx)): counts[idx[i]] += mnist.train.labels[i] # 把最频繁的标签分配到质心 labels_map = [np.argmax(c) for c in counts] labels_map = tf.convert_to_tensor(labels_map) # lookup:通过质心id映射到标签。 cluster_label = tf.nn.embedding_lookup(labels_map, cluster_idx) # 计算acc correct_prediction = tf.equal(cluster_label, tf.cast(tf.argmax(Y, 1), tf.int32)) accuracy_op = tf.reduce_mean(tf.cast(correct_prediction, tf.float32)) # 测试Model test_x, test_y = mnist.test.images, mnist.test.labels print("Test Accuracy:", sess.run(accuracy_op, feed_dict={ X: test_x, Y: test_y }))
X = tf.placeholder(tf.float32, shape=[None, num_features]) Y = tf.placeholder(tf.float32, shape=[None]) hparams = tensor_forest.ForestHParams(num_classes=num_classes, num_features=num_features, num_trees=num_trees, max_nodes=max_nodes).fill() # 构建随机森林 forgest_graph = tensor_forest.RandomForestGraphs(hparams) # 获取训练图和损失 train_op = forgest_graph.training_graph(X, Y) loss_op = forgest_graph.training_loss(X, Y) # 衡量准确率 infer_op, _, _ = forgest_graph.inference_graph(X) correct_prediction = tf.equal(tf.argmax(infer_op, 1), tf.cast(Y, tf.int64)) accuracy_op = tf.reduce_mean(tf.cast(correct_prediction, tf.float32)) init_vars = tf.group( tf.global_variables_initializer(), resources.initialize_resources(resources.shared_resources())) sess = tf.Session() sess.run(init_vars) for i in range(1, num_steps + 1): batch_x, batch_y = mnist.train.next_batch(batch_size) _, l = sess.run([train_op, loss_op], feed_dict={X: batch_x, Y: batch_y}) if i % 50 == 0 or i == 1: acc = sess.run(accuracy_op, feed_dict={X: batch_x, Y: batch_y})
def accuracy_fn(inference_fn, inputs, labels): prediction = tf.nn.softmax(inference_fn(inputs)) correct_pred = tf.equal(tf.argmax(prediction, 1), labels) return tf.reduce_mean(tf.cast(correct_pred, tf.float32))
def accuarcy_fn(interface_fn,inputs,labels): prediction = tf.nn.softmax(interface_fn(inputs)) correct_pred = tf.equal(tf.argmax(prediction,1),labels) # 计算预测值和标签是否相等 return tf.reduce_mean(tf.cast(correct_pred,tf.float32))