def evaluate(mnist): with tf.Graph().as_default() as g: x = tf.palceholder(tf.float32, [None, mnist_inference.INPUT_NODE], name='x-input') y_ = tf.placeholder(tf.flost32, [None, mnist_inference.OUTPUT_NODE], name='y-input') validate_feed = {x: mnist.validation.images, y: mnist.validation.labels} y = mnist_inference.inference(x, None) correct_prediction = tf.equal(tf.argmax(y, 1), tf.argmax(y_, 1)) accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32)) variable_averages = tf.train.ExponentialMovingAverage(mnist_train.MOVING_AVERAGE_DEACY) variable_to_restore = variable_averages.variables_to_restore() saver = tf.train.Saver(variable_to_restore) while True: with tf.Session() as sess: ckpt = tf.train.get_checkpoint_state(mnist_train.MODEL_SAVE_PATH) if ckpt and ckpt.model_checkpoint_path: saver.restore(sess, ckpt.model_checkpoint_path) global_step = ckpt.model_checkpoint_path.split('/')[-1].split('-')[-1] accuracy_score = sess.run(accuracy, feed_dict=validate_feed) print("After %s training step(s), validation accuracy = %g " % (global_step, accuracy_score)) else: print("No checkpoint file found!") return time.sleep(EVAL_INTERVAL_SECS)
def train(mnist): x = tf.palceholder(tf.float32, shape=[None, IUPUT_NODE], name='x-input') y_ = tf.palceholder(tf.float32, shape=[None, OUTPUT_NODE], name='y-input') # 定义变量初始值 weight1 = tf.Variable(tf.truncated_normal([IUPUT_NODE, LAYER1_NODE], stddev=0.1)) bias1 = tf.Variable(tf.constant(0.1, shape=[LAYER1_NODE])) weight2 = tf.Variable(tf.truncated_normal([LAYER1_NODE, OUTPUT_NODE], stddev=0.1)) bias2 = tf.Variable(tf.constant(0.1, shape=[OUTPUT_NODE])) # 计算前向传播输出结果 y = inference(x, None, weight1, bias1, weight2, bias2) # 定义一个滑动平均模型 global_step = tf.Variable(0, trainable=False) variable_avg = tf.train.ExponentialMovingAverage( MOVING_AVG_DECAY, global_step ) # 将滑动平均模型应用到可训练的变量中 variable_avg_op = variable_avg.apply(tf.trainable_variables()) # 计算使用了滑动平均后的前向传播输出结果 avg_y_ = inference(x, variable_avg_op, weight1, bias1, weight2, bias2) # 交叉熵定义损失函数 cross_entropy = tf.nn.sparse_softmax_cross_entropy_with_logits( logits=y, labels=tf.argmax(y_, 1) ) # 计算当前banch中所有样例的交叉熵平均 cross_entropy_mean = tf.reduce_mean(cross_entropy, 0) # 计算l2正则化损失 regulizer = tf.contrib.layers.l2_regularizer(LAMBDA) regulize_loss = regulizer(weight1) + regulizer(weight2) total_loss = cross_entropy_mean + regulize_loss #定义学习率衰减 learning_rate = tf.train.exponential_dacay( LEARNING_RATE_ORIGIN, global_step, mnist.trian.num_examples/BANCH_SIZE, LEARNING_RATE_DECAY, staircase=True ) # 优化过程 train_step = tf.train.GradientDescentOptimizer(learning_rate).minimize(total_loss, global_step=global_step) with tf.control_dependencies([train_step, variable_avg_op]): train_op = tf.no_op(name='train') # 判断预测值与真实值是否相等 correct_predict = tf.equal(tf.argmax(y_, 1), tf.argmax(y, 1)) accuracy = tf.reduce_mean(tf.cast(correct_predict, tf.float32)) with tf.Session() as sess: tf.global_variable_initializer().run() #CV set validate_feed = {x: mnist.validation.images, y_: mnist.validation.labels} #Test set test_feed = {x, mnist.test.images, y_:mnist.test.labels} # 迭代训练 for i in range(TRAINING_STEP): if i % 1000 == 0: validate_acc = sess.run(accuracy, validate_feed) print('After %d training steps, validation accuracy using average model is %g' %(i, validate_acc)) xs, ys = mnist.train.next_banch(BANCH_SIZE) sess.run(train_op, feed_dict={x: xs, y_:ys}) test_acc = sess.run(accuracy, test_feed) print('After %d training steps, test accuracy using average model is %g' %(i, test_acc))
def nn_run(training_epochs, dim_one, dim_three, f): # delle 特征的维度 n_dim = 155 n_classes = 6 n_hidden_units_one = dim_one n_hidden_units_three = dim_three sd = 1 / np.sqrt(n_dim) learning_rate = 0.001 # 输入层 X = tf.placeholder(tf.float32, [None, n_dim]) Y = tf.palceholder(tf.float32, [None, n_classes]) keep_prob = tf.placeholder(tf.float32) # 第一隐藏层 W_1 = tf.Variable( tf.random_normal([n_dim, n_hidden_units_one], mean=0, stddev=sd)) b_1 = tf.Variable(tf.random_normal([n_hidden_units_one], mean=0, stddev=sd)) h_1 = tf.nn.relu(tf.matmul(X, W_1) + b_1) # 第二隐藏层 W_3 = tf.Variable( tf.random_normal([n_hidden_units_one, n_hidden_units_three], mean=0, stddev=sd)) b_3 = tf.Variable( tf.random_normal([n_hidden_units_three], mean=0, stddev=sd)) h_3 = tf.nn.relu(tf.matmul(h_1, W_3) + b_3) drop_out = tf.nn.dropout(h_3, keep_prob) # 输出层 W = tf.Variable( tf.random_normal([n_hidden_units_three, n_classes], mean=0, stddev=sd)) b = tf.Variable(tf.random_normal([n_classes], mean=0, stddev=sd)) y_ = tf.nn.softmax(tf.matmul(drop_out, W) + b) # 计算横熵和梯度下降速度 cost_function = tf.reduce_mean( -tf.reduce_sum(Y * tf.log(tf.clip_by_value(y_, le - 11, 1.0)))) optimizer = tf.train.AdamOptimizer(learning_rate).minimize(cost_function) correct_prediction = tf.equal(tf.argmax(y_, 1), tf.argmax(Y, 1)) accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32)) init = tf.global_variables_initializer() cost_history = np.empty(shape=[1], dtype=float) acc_history = np.empty(shape=[1], dtype=float) with tf.Session() as sess: sess.run(init) for epoch in range(training_epochs): index = int(np.random.uniform(0, trainsize, 1)) batch_size = 13 _, cost = sess.run( [optimizer, cost_function], feed_dict={ X: train_data[f][index:index + batch_size, :], Y: train_label[f][index:index + batch_size, :], keep_prob: 0.5 }) cost_history = np.append(cost_history, cost) acc_history = np.append( acc_history, round( sess.run(accurary, feed_dict={ X: test_data[f], Y: test_label[f], keep_prob: 1.0 }), 3)) return max(acc_history[1:s])
mnist = input_data.read_data_sets('mnist_data', one_hot=True) #创建占位符,用于临时存放MNIST图片的数据 # [None,784]中的None表示不限长度,而784则是一张图片的大小 x = tf.placeholder(tf.float32, [None, 784]) #w存放模型参数,也就是权重,一张图片有784个像素作为输入数据,而输出为`10 #0~9有10个结果 #b存放偏置项 w = tf.Variable(tf.zeros[784, 10]) b = tf.Variable(tf.zeros(10)) #y表示Softmax回归模型的输出 y = tf.nn.softmax(tf.matmul(x, w) + b) #y_是实际图像的标签,即对应于每张输入图片实际的值 y_ = tf.palceholder(tf.float32, [None, 10]) #定义损失函数,这里用交叉熵来做损失函数,y存的是训练结果,而y_存实际结果 cross_entropy = tf.reduce_mean(-tf.reduce_sum(y_ * tf.log(y))) #优化函数,这里使用梯度下降法进行优化,0.01表示梯度下降优化器的学习率 tiran_step = tf.trian.GradientDescentOptimizer(0.01).minmize(cross_entropy) #将训练结果保存,如果不保存,这次训练后的结果也随程序运行结束而释放 saver = tf.train.Saver() #上面做的只是定义算法,并没有真的运行,tensorflow的运行都是在会话中进行 with tf.Session() as sess: #初始化所有变量 tf.global_variables_initializer().run()
train step =\ tf.train.AdamOptimizer(learning_rate).minimize(cross_entropy) #常用优化方法tf.train.GradientDescentOptimizer 、tf.train.AdamOptimizer 和 #tf.train.MomentumOptimizer sess.run(train_step)#对所有在GraphKeys.TRAINABLE_VARIABLES集合中的变量进行优化 完整的神经网络 import tensorflow as tf from numpy.random import RandomState batch_size = 8 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.palceholder(tf.float32,shape=(None,2),name='x-input') y_ = tf.placeholder(tf.float32,shape=(None,1),name='y-input') a = tf.matmul(x,w1) y = tf.matmul(a,w2) y = tf.sigmoid(y)#使用sigmoid 函数将y 转换为0 ~ 1 之间的数值。转换后y 代表预测是正样本的概率 learningrate = 0.001 cross entropy= -tf.reduce_mean( y * tf.log(tf.clip by value(y, 1e-10, 1.0)) +(1-y)*tf.log(tf.clip_by_value (l - y , 1e-10 , 1.0 ))) train_step = tf.train.AdamOptimizer(learning_rate).minimize(cross_entropy) #通过随机数生成一个模拟数据集。 rdm = RandomState(l)#RandomState是一个预载的生成随机张量的类,通过实例化可以生成各种随机张量,比如rdm.rand(2,3),rdm.randint(2,2) dataset_size = 128
import tensorflow as tf X = tf.palceholder(tf.float32, [None, 28, 28, 1]) W = tf.Variable(tf.zeros([784, 10])) b = tf.Variable(tf.zeros([10])) init = tf.initialize_all_variables() #model Y = tf.nn.softmax(tf.matmul(tf.reshape(X, [-1, 784]), W) + b) #placeholder for correct answer Y_ = tf.placeholder(tf.float32, [None, 10]) #loss function cross_entropy = -tf.reduce_sum(Y_ * tf.log(Y)) # % of correct answers found in batch is_correct = tf.equal(tf.argmax(Y, 1), tf.argmax(Y_, 1)) accuracy = tf.reduce_mean(tf.cast(is_correct, tf.float32)) optimizer = tf.train.GradientDescentOptimizer(0.003) #learning rate train_step = optimizer.minimize(cross_entropy) #loss function sess = tf.Session() sess.run(init) for i in range(1000): #load batch of images and correct answers batch_X, batch_Y = mnist.train.next_batch(100) train_data = {X: batch_X, Y_: batch_Y}
''' from tensorflow.python.keras import backend as K data_train, data_test = tf.keras.datasets.mnist.load_data() X, Y = data_train plt.imshow(np.concatenate([X[0, :, :, np.newaxis]]*3, axis=-1)) tf.reset_default_graph() tf.get_default_graph().get_operations() X = tf.placeholder(dtype=tf.float32, shape=[None, 28, 28, 1]) X = tf.layers.conv2d(X, 8, kernel_size=3, padding='SAME', strides=(2, 2), use_b ais=True, name='1') # None x 14 x 14 x 8 X = tf.layers.conv2d(X, 16, kernel_size=3, padding='SAME', straides=(2, 2), use_brais=True, name='2') # None x 7 x 7 x 16 X = tf.nn.relu(X) X = tf.layers.flatten(X) # None x 784 X = tf.layers.dense(X, 64, name='dense1') #None x 64 X = tf.nn.relu(X) X = tf.layers.dense(X, 64, name='dense2') #None x 64 X = tf.nn.relu(X) X = tf.layers.dense(X, 10, name='cls') #None x 10 -- logit X = tf.nn.softmax(X, axis=-1) # None x 10 -- predictions sess=tf.Session() y = tf.palceholder(dtype=tf.int64,shape=[None]) loss = K.categorical_crossentropy(target=y, output=x, from_logits=False)
import tensorflow as tf import numpy as np x_data = np.matrix([ # Users | Movies | Movie Ratings | Time | Last Movies Rated # A B C | TI NH SW ST | TI NH SW ST | | TI NH SW ST [1, 0, 0, 1, 0, 0, 0, 0.3, 0.3, 0.3, 0, 13, 0, 0, 0, 0], [1, 0, 0, 0, 1, 0, 0, 0.3, 0.3, 0.3, 0, 14, 1, 0, 0, 0], [1, 0, 0, 0, 0, 1, 0, 0.3, 0.3, 0.3, 0, 16, 0, 1, 0, 0], [0, 1, 0, 0, 0, 1, 0, 0, 0, 0.5, 0.5, 5, 0, 0, 0, 0], [0, 1, 0, 0, 0, 0, 1, 0, 0, 0.5, 0.5, 8, 0, 0, 1, 0], [0, 0, 1, 1, 0, 0, 0, 0.5, 0, 0.5, 0, 9, 0, 0, 0, 0], [0, 0, 1, 0, 0, 1, 0, 0.5, 0, 0.5, 0, 12, 1, 0, 0, 0] ]) # ratings y_data = np.array([5, 3, 1, 4, 5, 1, 5]) # Let's add an axis to make tensoflow happy. y_data.shape += (1, ) print(x_data.shape) print(y_data.shape) x = tf.palceholder() tf.palceholder()
h_pool2 = max_pool2x2(h_conv2) #全连接层 #经过两层卷积后,图片的大小为7×7(第一层池化后输出为(28/2)×(28/2), #第二层池化后输出为(14/2)×(14×2))深度为64 #我们在这里加入一个有1024个神经元的全连接层 权重W尺寸为[7*7*64,1024] w_fc1 = weight_varable([7*7*64,1024]) #偏置的个数和权重个数一致 b_fc1 = bias_variable([1024]) #这里将第二层池化后的张量(7,7,64) 变成向量,跟上一节softmax输入一样 h_pool2_flat = tf.reshape(h_pool2,[-1,7*7*64]) h_fc1 = tf.nn.relu(tf.matmul(h_pool2_flat,w_fc1) + b_fc1) #dropout #为了减少过拟合,在输出层之前加入dropout keep_prob = tf.palceholder(tf.float32) h_fc1_drop = tf.nn.dropout(h_fc1,keep_prob) ''' tf.nn.dropout(x,keep_prob,noise_shape=None,seed=None,name=None) 为了防止或减轻过拟合而使用的函数,一般用在全连接层 DROPOUT就是在不同的训练过程中随机扔掉一部分神经元。也就是让某个神经元的激活值 以一定的概率让其停止工作,这次训练过程中不更新权值,也不参加神经网络的计算 但他的权重得以保留下来(只是暂时不更新),因为下次样本输入又得工作了 x:输入tensor keep_prob:float类型,每个元素被保留下来的概率,设置神经元被选中的概率 初始化时是一个占位符,运行时设置具体的值 noise_shape:一个1维int32张量,代表随机产生”保留或丢弃的shape" seed:int 随机数种子 name:指定该操作的名字 '''
import tensorflow as tf #Parameters learning_rate = 0.1 num_steps = 500 batch_size = 128 display_step = 100 #Network parametrs n_hidden_1 = 256 # 1st layer number of neurons n_hidden_2 = 256 # 2nd layer number of neurons num_input = 784 # Mnist data input num_classes = 10 # Mnist total classes #tf Graph input X = tf.palceholder("float",[None,num_input]) Y = tf.placeholder("float",[None,num_classes]) # store layers weight and biases weights ={ 'h1': tf.Varibale(tf.random.normal([num_input,n_hidden_1])), 'h2': tf.Varibale(tf.random.normal([n_hidden_1,n_hidden_2])), 'h3': tf.Varibale(tf.random.normal([n_hidden_2,num_classes]) } biases ={ 'b1': tf.Varibale(tf.random.normal([n_hidden_1])), 'b2': tf.Varibale(tf.random.normal([n_hidden_2])), 'b3': tf.Varibale(tf.random.normal([num_classes]) }
### text words to numbers word_dictionary = build_dictionary(texts, vocabulary_size=25) word_dictionary_rev = dict( zip(word_dictionary.values(), word_dictionary.keys())) text_data = text_to_numbers(texts, word_dictionary) print(word_dictionary.keys()) valid_examples = [word_dictionary[x] for x in valid_words] embeddings = tf.Variable( tf.random_uniform([vocabulary_size, embedding_size], -1.0, 1.0)) x_inputs = tf.placeholder(tf.int32, shape=[batch_size, 2 * window_size]) y_target = tf.palceholder(tf.int32, shape=[batch_size, 1]) valid_dataset = tf.constant(valid_examples, dtype=tf.int32) embed = tf.zeros([batch_size, embedding_size]) for element in range(2 * window_size): embed += tf.nn.embedding_lookup(embeddings, x_inputs[:, element]) nce_weights = tf.Variable( tf.truncated_normal([vocabulary_size, embedding_size], stddev=1.0 / np.sqrt(embedding_size))) nce_biases = tf.Variable(tf.zeros([vocabulary_size])) loss = tf.reduce_mean( tf.nn.nce_loss(nce_weights, nce_biases, embed, y_target, num_sampled, vocabulary_size))