def test(mnist): with tf.Graph().as_default() as graph: x = tf.placeholder(tf.float32, shape = [None,forward.INPUT_NODE]) y_ = tf.placeholder(tf.float32, shape = [None,forward.OUTPUT_NODE]) y = forward.forward(x,backward.REGULARIZER) ema = tf.train.ExponentialMovingAverage(backward.MOVING_AVERAGE_DECAY) ema_restore = ema.variables_to_restore() saver = tf.train.Saver(ema_restore) correct = tf.equal(tf.argmax(y, 1), tf.argmax(y_, 1)) accuracy = tf.reduce_mean(tf.cast(correct, tf.float32)) while True: with tf.Session() as sess: ckpt = tf.train.get_checkpoint_state(backward.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 = {x:mnist.test.images, y_:mnist.test.labels}) print("after %s setps, test accuracy is %g"%(global_step, accuracy_score)) else: print("no checkpoint") return time.sleep(TEST_INTERVAL)
def backward(): # 给输入x和真实标签y_占位 x = tf.placeholder(tf.float32, [None,128,128,3]) y_ = tf.placeholder(tf.float32, [None, 6 ]) # 由前向传播得到预测值 y = forward.forward(x, REGULARIZER) global_step = tf.Variable(0, trainable=False) #定义均方误差 ce = tf.nn.sparse_softmax_cross_entropy_with_logits(logits=y, labels=tf.argmax(y_, 1)) cem = tf.reduce_mean(ce) # 定义损失,loss等于均方误差加上l2正则化误差 loss = cem + tf.add_n(tf.get_collection('losses')) #定义指数衰减学习率学习率 learning_rate = tf.train.exponential_decay( LEARNING_RATE_BASE, global_step, train_num_examples / BATCH_SIZE, LEARNING_RATE_DECAY, staircase=True) #定义反向传播算法 train_step = tf.train.GradientDescentOptimizer(learning_rate).minimize(loss, global_step=global_step) #定义滑动平均 ema = tf.train.ExponentialMovingAverage(MOVING_AVERAGE_DECAY, global_step) ema_op = ema.apply(tf.trainable_variables()) with tf.control_dependencies([train_step, ema_op]): train_op = tf.no_op(name='train') #实例化saver saver = tf.train.Saver() #从generateds.py文件得到批量数据 img_batch, label_batch = generateds.readTFReccord(TFR_save_dir) #开始图计算 with tf.Session() as sess: init_op = tf.global_variables_initializer() sess.run(init_op) #实例化ckpt,在路径MODEL_SAVE_PATH下查找模型,如果有则加载模型 ckpt = tf.train.get_checkpoint_state(MODEL_SAVE_PATH) if ckpt and ckpt.model_checkpoint_path: saver.restore(sess, ckpt.model_checkpoint_path) #开启线程协调器 coord = tf.train.Coordinator() threads = tf.train.start_queue_runners(sess=sess, coord=coord) #进行训练 for i in range(STEPS): xs, ys = sess.run([img_batch, label_batch]) _, loss_value, step = sess.run([train_op, loss, global_step], feed_dict={x: xs, y_: ys}) if i % 1000 == 0: print("After %d training step(s), loss on training batch is %g." % (step, loss_value)) #每训练1000次保存模型 saver.save(sess, os.path.join(MODEL_SAVE_PATH, MODEL_NAME), global_step=global_step) #关闭线程协调器 coord.request_stop() coord.join(threads)
def to_uff(model_path, pb_model_file, uff_model_file): forward.self_print("to uff[%s]" % (model_path)) with tf.Graph().as_default() as g: # 定义输入占位 #x = tf.placeholder(tf.float32, [None, forward.INPUT_NODE], name='x-input') x = tf.compat.v1.placeholder( tf.float32, [32, forward.NUM_CHANNELS, forward.IMAGE_SZIE, forward.IMAGE_SZIE], name='x-input') # 因输入的是NCHW 需转成NHWC x_image = tf.transpose(x, [0, 2, 3, 1]) # 前向传播,因验证时无需关注正则化损失值 y = forward.forward(x_image, False, None) inference = tf.add(x=0.0, y=y, name='inference') """ 通过变量重命名的方式来加载模型,这样在前向传播的过程中就不需要调用滑动 平均的函数获取平均值了。这样就可以完全共用前向传播过程了。 通过使用variables_to_restore函数,可以使在加载模型的时候将影子变量直接 映射到变量的本身,所以我们在获取变量的滑动平均值的时候只需要获取到变量 的本身值而不需要去获取影子变量 """ var_averages = tf.compat.v1.train.ExponentialMovingAverage( train.MOVING_AVERAGE_DECAY) var_to_restore = var_averages.variables_to_restore() saver = tf.compat.v1.train.Saver(var_to_restore) config = tf.compat.v1.ConfigProto() config.gpu_options.allow_growth = True with tf.Session(config=config) as sess: if model_path: saver.restore(sess, model_path) forward.self_print("pb_model_file[%s]" % (pb_model_file)) [save_path, save_file] = os.path.split(pb_model_file) graph_io.write_graph(sess.graph, save_path, "tmp_graph.pb") freeze_graph.freeze_graph(save_path + "/tmp_graph.pb", '', False, model_path, "inference", "save/restore_all", "save/Const:0", pb_model_file, False, "") # 转成uff uff.from_tensorflow_frozen_model( pb_model_file, output_nodes=[], preprocessor=None, input_node=[], quiet=False, text=False, list_nodes=False, output_filename=uff_model_file, write_preprocessed=False, debug_mode=False)
def train(inputLayer, target, weight1, weight2, hiddenNodes, epochs, learningRate): print("Starting training.") for epoch in range(epochs): # train on training set error = np.zeros(1) for index in range(1): # run the forward function hiddenLayer, outputLayer = forward(inputLayer, weight1, weight2) print("Hidden layer:") print(hiddenLayer) print("Output layer:") print(outputLayer) # save the error error[index] = 1/2 * (outputLayer - target)**2 print("Error: {:.8f}.".format(error[index])) errorDelta = outputLayer - target # backpropagation outputDelta = backpropagation.backward(outputLayer, 1, errorDelta) print("Output delta: {:.8f}.".format(outputDelta)) weight2 = backpropagation.updateWeight(hiddenLayer, weight2, outputDelta, learningRate) print("weight 2:") print(weight2) hiddenDelta = backpropagation.backward(hiddenLayer, weight2, outputDelta) # bias is not a part of calculating the weights for the input hiddenDelta = hiddenDelta[0,0:hiddenNodes] print("hidden delta:") print(hiddenDelta) weight1 = backpropagation.updateWeight(inputLayer, weight1, hiddenDelta, learningRate) print("weight 1:") print(weight1) print("Training complete.") return weight1, weight2
def backward(): x = tf.placeholder(tf.float32, shape=(None, 2)) y_ = tf.placeholder(tf.float32, shape=(None, 1)) X, Y_, Y_c = generateds.generateds() y = forward.forward(x, REGULARIZER) global_step = tf.Variable(0, trainable=False) learning_rate = tf.train.exponential_decay(LEARNING_RATE_BASE, global_step, 300 / BATCH_SIZE, LEARNING_RATE_DECAY, staircase=True) #定义损失函数 loss_mse = tf.reduce_mean(tf.square(y - y_)) loss_total = loss_mse + tf.add_n(tf.get_collection('losses')) #反向传播方法 包含正则化 train_step = tf.train.AdamOptimizer(learning_rate).minimize(loss_total) #生成会话,训练STEPS轮 with tf.Session() as sess: init_op = tf.global_variables_initializer() sess.run(init_op) for i in range(STEPS): start = (i * BATCH_SIZE) % 300 end = start + BATCH_SIZE sess.run(train_step, feed_dict={ x: X[start:end], y_: Y_[start:end] }) if i % 2000 == 0: loss_V = sess.run(loss_total, feed_dict={x: X, y_: Y_}) print("After %d step(s), loss is: %g" % (i, loss_V)) #xx在-3到3之间以步长为0.01,yy在-3到3之间以步长0.01,生成二维网格坐标点 xx, yy = np.mgrid[-3:3:.01, -3:3:.01] #将xx,yy拉直,并合成一个2列的矩阵,得到一个网格坐标点的集合 grid = np.c_[xx.ravel(), yy.ravel()] #将网格坐标点喂入神经网络,probs为输出 probs = sess.run(y, feed_dict={x: grid}) #probs的shape调整成xx的样子 probs = probs.reshape(xx.shape) plt.scatter(X[:, 0], X[:, 1], c=np.squeeze(Y_c)) plt.contour(xx, yy, probs, levels=[.5]) plt.show()
def backward(inputx,labely): x=tf.placeholder(tf.float32,[BATCH_SIZE,forward.IMAGE_SIZE,forward.IMAGE_SIZE,forward.NUM_CHANNELS]) y_ = tf.placeholder(tf.float32,[None,forward.OUTPUT_NODE]) y=forward.forward(x,True,REGULARIZER) #true说明在训练时开启dropout global_step=tf.Variable(0,trainable=False) ce=tf.nn.sparse_softmax_cross_entropy_with_logits(logits=y,labels=tf.argmax(y_,1)) cem=tf.reduce_mean(ce) loss=cem+tf.add_n(tf.get_collection('losses')) learning_rate = tf.train.exponential_decay(LEARNING_RATE_BASE,global_step,50000/BATCH_SIZE,LEARNING_RATE_DECAY,staircase=True) train_step = tf.train.GradientDescentOptimizer(learning_rate).minimize(loss,global_step=global_step) ema=tf.train.ExponentialMovingAverage(MOVING_AVERAGE_DECAY,global_step) ema_op=ema.apply(tf.trainable_variables()) with tf.control_dependencies([train_step,ema_op]): train_op=tf.no_op(name='train') saver=tf.train.Saver() config = tf.ConfigProto() config.gpu_options.allow_growth = True ''' 爆我显存?cnm ''' with tf.Session(config=config) as sess: init_op=tf.global_variables_initializer() sess.run(init_op) ckpt=tf.train.get_checkpoint_state(MODEL_SAVE_PATH) if ckpt and ckpt.model_checkpoint_path: saver.restore(sess,ckpt.model_checkpoint_path) for i in range(STEPS): start=(i*BATCH_SIZE)%50000 end=start+BATCH_SIZE if end >50000: continue xs=inputx[start:end] ys=labely[start:end] #reshaped_xs=np.reshape(xs,(BATCH_SIZE,forward.IMAGE_SIZE,forward.IMAGE_SIZE,forward.NUM_CHANNELS)) _,loss_value,step=sess.run([train_op,loss,global_step],feed_dict={x:xs,y_:ys}) if i%100 ==0: print("After %d steps, loss is %g" %(step,loss_value)) #saver.save(sess,os.path.join(MODEL_SAVE_PATH,MODEL_NAME),global_step=global_step) if i%25000==0: saver.save(sess,os.path.join(MODEL_SAVE_PATH,MODEL_NAME),global_step=global_step)
def backward(mnist): x = tf.placeholder(tf.float32, [None, forward.INPUT_NODE]) y_ = tf.placeholder(tf.float32, [None, forward.OUTPUT_NODE]) y = forward.forward(x, REGULARIZER) global_step = tf.Variable(0, trainable=False) ce = tf.nn.sparse_softmax_cross_entropy_with_logits(logits=y, labels=tf.argmax( y_, 1)) cem = tf.reduce_mean(ce) loss = cem + tf.add_n(tf.get_collection('losses')) learning_rate = tf.train.exponential_decay(LEARNING_RATE_BASE, global_step, mnist.train.num_examples / BATCH_SIZE, LEARNING_RATE_DECAY, staircase=True) train_step = tf.train.GradientDescentOptimizer(learning_rate).minimize( loss, global_step=global_step) ema = tf.train.ExponentialMovingAverage(MOVING_AVERAGE_DECAY, global_step) ema_op = ema.apply(tf.trainable_variables()) with tf.control_dependencies([train_step, ema_op]): train_op = tf.no_op(name='train') saver = tf.train.Saver() with tf.Session() as sess: init_op = tf.global_variables_initializer() sess.run(init_op) ckpt = tf.train.get_checkpoint_state(MODEL_SAVE_PATH) if ckpt and ckpt.model_checkpoint_path: saver.restore(sess, ckpt.model_checkpoint_path) for i in range(STEPS): xs, ys = mnist.train.next_batch(BATCH_SIZE) _, loss_value, step = sess.run([train_op, loss, global_step], feed_dict={ x: xs, y_: ys }) if i % 1000 == 0: print( "After %d training step(s), loss on training batch is %g." % (step, loss_value)) saver.save(sess, os.path.join(MODEL_SAVE_PATH, MODEL_NAME), global_step=global_step)
def backward(): x = tf.placeholder(tf.float32, shape=(None, 2)) y_ = tf.placeholder(tf.float32, shape=(None, 1)) # 读样本 X, Y_, Y_c = cd.generateds() # 计算前向传播的结果 y = fd.forward(x, REGULARIZER) # 定义损失函数 loss_mes = tf.reduce_mean(tf.square(y - y_)) loss_total = loss_mes + tf.add_n(tf.get_collection('losses')) # 定义指数衰减学习率 global_step = tf.Variable(0, trainable=False) learning_rate = tf.train.exponential_decay(LEARNING_RATE_BASE, global_step, 300 / BATCH_SIZE, LEARNING_RATE_DECAY, staircase=True) # 定义训练步骤 train_step = tf.train.AdadeltaOptimizer(learning_rate).minimize(loss_total) with tf.Session() as sess: init_op = tf.global_variables_initializer() sess.run(init_op) for i in range(STEP): start = (i * BATCH_SIZE) % 300 end = start + BATCH_SIZE sess.run(train_step, feed_dict={ x: X[start:end], y_: Y_[start:end] }) if i % 2000 == 0: loss = sess.run(loss_mes, feed_dict={x: X, y_: Y_}) print("after %d steps, loss is : %f" % (i, loss)) # 产生网格坐标 xx, yy = np.mgrid[-3:3:.01, -3:3:.01] grid = np.c_[xx.ravel(), yy.ravel()] predict = sess.run(y, feed_dict={x: grid}) predict = predict.reshape(xx.shape) # 绘制图像 plt.scatter(X[:, 0], X[:, 1], c=np.squeeze(Y_c)) plt.contour(xx, yy, predict, levels=[0.5]) plt.show()
def test(): X = tf.placeholder(tf.float32, [None, None, None, 3]) #输入(黑白图片)的占位符 with tf.name_scope('generator'), tf.variable_scope( 'generator'): #生成器的变量名前加上generator前缀,以便与判别器的变量分开训练 Y = forward.forward(X, 1, False) #构建生成器网络,并获得其输出与中间层 Y_real = tf.placeholder(tf.float32, [None, None, None, 3]) #以中间层为输入构建guide decoder XYY = tf.concat([X, Y, Y_real], axis=2) #输出(输入对应的原彩色图片)的占位符 global_step = tf.Variable(0, trainable=False) #定义global step saver = tf.train.Saver() #定义用来读取模型的saver X_batch, Y_real_batch = generateds.get_tfrecord( 1, False) #从tfrecord中获取黑白图和对应彩图 with tf.Session() as sess: sess.run(tf.global_variables_initializer()) #全局变量初始化 ckpt = tf.train.get_checkpoint_state( backward.MODEL_SAVE_PATH) #在模型存放路径中获取模型checkpoint的状态 if ckpt and ckpt.model_checkpoint_path: #如果存在checkpoint且可以获得其最新版本的路径 saver.restore(sess, ckpt.model_checkpoint_path) #从模型的最新版本路径读取模型中的参数 else: #没找到checkpoint话 print('Checkpoint Not Found') #输出一下 return #没有模型可以测试,结束运行 coord = tf.train.Coordinator() #创建一个coordinator threads = tf.train.start_queue_runners(sess=sess, coord=coord) #创建读取数据的线程们 if not os.path.exists(TEST_RESULT_PATH): #创建需要但尚未创建的训练结果目录 os.mkdir(TEST_RESULT_PATH) #创建需要但尚未创建的训练结果目录 for i in range(TEST_NUM): #对于每一轮测试 xs, ys = sess.run([X_batch, Y_real_batch ]) #从tfrecord中读取x和y的下一批数据(对于测试来说,一批就是一张) img = sess.run(XYY, feed_dict={ X: xs, Y_real: ys }) #获取黑白图、生成图、原材图的拼接 img = (img + 1) / 2 #从-1~1映射到0~1 img *= 256 #再映射到0~256 img = img.astype(np.uint8) #类型化为uint8 Image.fromarray(img[0]).save( os.path.join(TEST_RESULT_PATH, '{}.png'.format(i + 1))) #转成图片并保存 coord.request_stop() #要求读取图片的线程们停止 coord.join(threads) #等待他们停止
def test(model_path, mnist): with tf.Graph().as_default() as g: # 定义输入输出占位 x = tf.placeholder(tf.float32, [None, forward.INPUT_NODE], name="x-input") y_ = tf.placeholder(tf.float32, [None, forward.OUTPUT_NODE], name="y-input") validate_feed = {x: mnist.test.images, y_: mnist.test.labels} # 前向传播,因测试时无需关注正则化损失值 y = forward.forward(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( train.MOVING_AVERAGE_DECAY) variables_to_restore = variable_averages.variables_to_restore() saver = tf.train.Saver(variables_to_restore) config = tf.ConfigProto() config.gpu_options.allow_growth = True with tf.Session(config=config) as sess: if (model_path): # 加载模型 saver.restore(sess, model_path) accuracy_score, x1, y1, y_1 = sess.run([accuracy, x, y, y_], feed_dict=validate_feed) print("x shape:", x1.shape) print("y shape:", y1.shape) print("y_ shape:", y_1.shape) #print("y0:", y1[0]) #print("y_0:", y_1[0]) #print("x2:", x1[2]) #img = x1[0].reshape(28,28) #scipy.misc.toimage(img, cmin=0.0,cmax=1.0).save("/home/wangyinzhi/video/mnist_test_0.png") #for j in range(50): # img = x1[60+j].reshape(28,28) # file_path = "/home/wangyinzhi/video/mnist_test_" # file_path += str(j) # file_path += ".png" # print(file_path) # scipy.misc.toimage(img, cmin=0.0,cmax=1.0).save(file_path) #print("y_0:", y_[0].eval()) print("test accuracy_score:%g" % (accuracy_score)) else: print("model file[%s] not find" % (model_path))
def backward(): steps = 50000 batch_size = 30 lr_base = 0.001 lr_decay = 0.999 regularizer = 0.01 data_count = generate.data_count() global_step = tf.Variable(0, trainable=False) # nn x = tf.placeholder(tf.float32, shape=(None, 2)) y_ = tf.placeholder(tf.float32, shape=(None, 1)) mat_x, mat_y_, y_color = generate.generate() y = forward.forward(x, regularizer) lr = tf.train.exponential_decay(learning_rate=lr_base, global_step=global_step, decay_steps=data_count / batch_size, decay_rate=lr_decay, staircase=True) loss_mse = tf.reduce_mean(tf.square(y - y_)) loss_regular = loss_mse + tf.add_n(tf.get_collection("losses")) train_step = tf.train.AdamOptimizer(lr).minimize(loss_regular) with tf.Session() as sess: sess.run(tf.global_variables_initializer()) for i in range(steps): start = (i * batch_size) % data_count end = start + batch_size feeds = {x: mat_x[start:end], y_: mat_y_[start:end]} sess.run(train_step, feed_dict=feeds) if i % 5000 == 0: loss_mse_val = sess.run(loss_mse, feed_dict={ x: mat_x, y_: mat_y_ }) print("第%d步的损失为%f" % (i, loss_mse_val)) xx, yy = np.mgrid[-3:3:0.01, -3:3:0.01] grid = np.c_[xx.ravel(), yy.ravel()] probs = sess.run(y, feed_dict={x: grid}) probs = probs.reshape(xx.shape) plt.scatter(mat_x[:, 0], mat_x[:, 1], c=np.squeeze(y_color)) plt.contour(xx, yy, probs, levels=[0.5]) plt.show() return None
def backward(): #训练集占位 x = tf.placeholder(tf.float32, shape=(None, 2)) y_ = tf.placeholder(tf.float32, shape=(None, 1)) #导入数据集 X, Y_, Y_c = generateds.generateds() y = forward.forward(x, REGULARIZER) #轮数计数器 global_step = tf.Variable(0, trainable=False) #定义指数学习率 learn_rete = tf.train.exponential_decay(LEARNING_RATE_BASE, global_step, 300/BATCH_SIZE, LEARNING_RATE_DECAY, staircase=True) #定义损失函数 loss_mse = tf.reduce_mean(tf.square(y - y_)) loss_total = loss_mse + tf.add_n(tf.get_collection('losses')) #定义训练方法 train_step = tf.train.AdamOptimizer(learn_rete).minimize(loss_total) #创建会话图进行 with tf.Session() as sess: init_op = tf.global_variables_initializer() sess.run(init_op) for i in range(STEPS): start = (i*BATCH_SIZE) % 300 end = start + BATCH_SIZE sess.run(train_step, feed_dict={x:X[start:end], y_:Y_[start:end]}) if i % 2000 == 0: loss_v = sess.run(loss_total, feed_dict={x:X, y_:Y_}) print('After %d steps, loss_total is %f' % (i, loss_v)) #生成网格坐标点 xx, yy = np.mgrid[-3:3:.01, -3:3:.01] grid = np.c_[xx.ravel(), yy.ravel()] probs = sess.run(y, feed_dict={x:grid}) probs = probs.reshape(xx.shape) #可视化训练集 plt.scatter(X[:,0], X[:,1], c=np.squeeze(Y_c)) plt.contour(xx, yy, probs, levels=[.5]) plt.show()
def train(mnist): # 定义输入输出placeholder x = tf.placeholder( tf.float32, shape=[None, forward.INPUT_NODE], name="x-input") y_ = tf.placeholder( tf.float32, shape=[None, forward.OUTPUT_NODE], name="y-input") regularizer = tf.contrib.layers.l2_regularizer(REGULARAZTION_RATE) # 直接使用forward中定义的前向传播过程 y = forward.forward(x, regularizer) global_step = tf.Variable(0, trainable=False) # 定义损失函数、学习率、滑动平均以及训练过程 variable_averages = tf.train.ExponentialMovingAverage( MOVING_AVERAGE_DECAY, global_step) variables_averages_op = variable_averages.apply(tf.trainable_variables()) cross_entropy = tf.nn.sparse_softmax_cross_entropy_with_logits( logits=y, labels=tf.argmax(y_, 1)) cross_entropy_mean = tf.reduce_mean(cross_entropy) loss = cross_entropy_mean + tf.add_n(tf.get_collection('losses')) learning_rate = tf.train.exponential_decay( LEARNING_RATE_BASE, global_step, mnist.train.num_examples / BATCH_SIZE, LEARNING_RATE_DECAY) train_step = tf.train.GradientDescentOptimizer(learning_rate).minimize( loss, global_step=global_step) with tf.control_dependencies([train_step, variables_averages_op]): # control_dependencies 指定某些操作执行的依赖关系 # tf.no_op表示执行完train_step,variables_averages_op操作后什么都不做 train_op = tf.no_op(name='train') # 初始化Tensroflow持久化类 saver = tf.train.Saver() with tf.Session() as sess: sess.run(tf.global_variables_initializer()) ''' 在训练过程中不再测试模型在验证数据上的表现,验证和测试的过程将会有一个独立的程序来完成 ''' for i in range(TRAINING_STEPS): xs, ys = mnist.train.next_batch(BATCH_SIZE) _, loss_value, step = sess.run([train_op, loss, global_step], feed_dict={ x: xs, y_: ys }) # 每1000轮保存一次模型 if i % 1000 == 0: print("After %d training step,loss on training batch is %g." % (step, loss_value)) saver.save( sess, os.path.join(MODEL_SAVE_PATH, MODEL_NAME), global_step=global_step)
def recommendation(usr, ripple, N, entity, relationship, usr_history_dict): n_entity = len(entity) n_relation = len(relationship) #确定输入数据 put_h = [] put_r = [] put_t = [] x = tf.placeholder(dtype=tf.int32, shape=[None], name="items") for hop in range(hops): put_h.append( tf.placeholder(dtype=tf.int32, shape=[None, catch], name="h" + str(hop))) put_r.append( tf.placeholder(dtype=tf.int32, shape=[None, catch], name="h" + str(hop))) put_t.append( tf.placeholder(dtype=tf.int32, shape=[None, catch], name="h" + str(hop))) y_, _ = forward(x, put_h, put_r, put_t, n_entity, n_relation) recommendation = dict() #对每个物品判断推荐的概率 for item in range(n_entity): emm = [item] feed_dict = dict() feed_dict[x] = emm for i in range(hops): feed_dict[put_h[i]] = [ripple[usr][i][0]] feed_dict[put_r[i]] = [ripple[usr][i][1]] feed_dict[put_t[i]] = [ripple[usr][i][2]] sess = tf.InteractiveSession() saver = tf.train.Saver() saver.restore(sess, model_path) y = sess.run([y_], feed_dict=feed_dict) #不推荐已经观看的电影 if item not in usr_history_dict: if item not in recommendation.keys(): recommendation[item] = y result = sorted(recommendation.items(), key=lambda item: item[1], reverse=True) return result.keys[0:N]
def train(X_train, y_train, weight_inputToHidden, weight_hiddenToOutput, biasHidden, biasOutput, learningRate=1e-3, reg=1e-5, num_iters=100, verbose=False): # Run stochastic gradient descent to optimize W loss_history = [] num_training, dataSize = np.shape(X_train) # print(num_training) # batch_size = 500 for iterNum in range(num_iters): startTime = timeit.default_timer() for i in range(num_training): loss, grad_hiddenToOutput, grad_inputToHidden, grad_biasOutput, grad_biasHidden = backward.backward(X_train[i], y_train[i], weight_inputToHidden, weight_hiddenToOutput, biasHidden, biasOutput, reg) # update loss loss_history.append(loss) # update parameters weight_inputToHidden -= learningRate * grad_inputToHidden weight_hiddenToOutput -= learningRate * grad_hiddenToOutput biasHidden -= learningRate * grad_biasHidden biasOutput -= learningRate * grad_biasOutput stopTime = timeit.default_timer() if verbose and iterNum % 5 == 0: print('iteration %d / %d: loss %f' % (iterNum, num_iters, loss)) print('Time Taken : ', stopTime - startTime) output, classOutput = forward.forward(X_train, weight_inputToHidden, weight_hiddenToOutput, biasHidden, biasOutput) # print('Actual : ', y_train [1 : 10]) # print('Predicted :', classOutput [1:10]) print('Training accuracy: {:f}'.format(np.mean(y_train == classOutput))) # print('Iter ', iterNum, ' -- training accuracy: {:f}'.format(np.mean(y_train == classOutput))) return loss_history, weight_inputToHidden, weight_hiddenToOutput, biasHidden, biasOutput
def back(): x = tf.placeholder(tf.float32, shape=(None, 2)) y_ = tf.placeholder(tf.float32, shape=(None, 1)) X, Y_, Y_c = generateds.generateds() y = forward.forward(x, REGULARIZER) #-------2.定义损失函数及反向传播方法------- # 这里将之前的定长学习率改进成指数衰减型学习率。trainable=False用来声明global_step不是训练变量,一定要要。 # 指数衰减型学习率的计算公式:learning_rate = learning_rate_base * (learning_rate_decay^(global_step/(总样本数/BATCH_SIZE))) # 在训练模型时,使用指数衰减学习率可以使模型在训练的前期快速收敛接近较优解,又可以保证模型在训练后期不会有太大波动。 global_step = tf.Variable(0, trainable=False) # global_step=tf.Variable(0) # global_step=tf.Variable(0,trainable=True) # tf.train.exponential_decay是tensorflow中 learning_rate = tf.train.exponential_decay( LEARNING_RATE_BASE, global_step, 300 / BATCH_SIZE, # 总样本数/BATCH_SIZE LEARNING_RATE_DECAY, staircase=True) # 当staircase = True时,指数global_step/(总样本数/BATCH_SIZE)会取整;当staircase = False时,不取整,是光滑直线 #定义损失函数,用的MMSE均方误差+正则化。tf.get_collection是从一个集合中取出变量,把很多变量变成一个列表(向量):列表示例为[1.0,2.0,3.0] loss_mse = tf.reduce_mean(tf.square(y - y_)) # tf.add_n:把一个列表的东西都依次加起来,是一个常数 loss_total = loss_mse + tf.add_n(tf.get_collection('losses')) # 不可以将tf.add_n去掉,因为tf.get_collection后是[],tf.add_n后变成常数 #定义反向传播方法:包含正则化 train_step = tf.train.AdamOptimizer(learning_rate).minimize(loss_total) #-------3.生成对话,训练steps轮------- with tf.Session() as sess: init_op = tf.global_variables_initializer() sess.run(init_op) for i in range(STEPS): start = (i * BATCH_SIZE) % 300 end = start + BATCH_SIZE sess.run(train_step, feed_dict={x:X[start:end], y_:Y_[start:end]}) if i % 2000 == 0: loss_v = sess.run(loss_total, feed_dict={x:X, y_:Y_}) print("After %d steps,loss is:%f" % (i, loss_v)) xx,yy = np.mgrid[-3:3:0.01, -3:3:0.01] grid = np.c_[xx.ravel(), yy.ravel()] probs = sess.run(y, feed_dict={x:grid}) probs = probs.reshape(xx.shape) plt.scatter(X[:,0], X[:,1], c=np.squeeze(Y_c)) plt.contour(xx, yy, probs, levels=[.5]) plt.show()
def backward(mnist): x = tf.placeholder(tf.float32, [ BATCH_SIZE, forward.IMAGE_SIZE, forward.IMAGE_SIZE, forward.NUM_CHANNELS]) y_ = tf.placeholder(tf.float32, [None, forward.OUTPUT_NODE]) y = forward.forward(x, True, REGULARIZER) global_step = tf.Variable(0, trainable=False) ce = tf.nn.sparse_softmax_cross_entropy_with_logits(logits=y, labels=tf.argmax(y_, 1)) cem = tf.reduce_mean(ce) loss = cem + tf.add_n(tf.get_collection('losses')) learning_rate = tf.train.exponential_decay( LEARNING_RATE_BASE, global_step, mnist.train.num_examples/BATCH_SIZE, LEARNING_RATE_DECAY, staircase=True) train_step = tf.train.GradientDescentOptimizer(learning_rate).minimize(loss, global_step=global_step) ema = tf.train.ExponentialMovingAverage(MOVING_AVERAGE_DECAY, global_step) ema_op = ema.apply(tf.trainable_variables()) with tf.control_dependencies([train_step, ema_op]): train_op = tf.no_op(name='train') saver = tf.train.Saver() with tf.Session() as sess: init_op = tf.global_variables_initializer() sess.run(init_op) ckpt = tf.train.get_checkpoint_state(MODEL_SAVE_PATH) if ckpt and ckpt.model_checkpoint_path: saver.restore(sess, ckpt.model_checkpoint_path) for i in range(STEPS): xs, ys = mnist.train.next_batch(BATCH_SIZE) reshaped_xs = np.reshape(xs,( BATCH_SIZE, forward.IMAGE_SIZE, forward.IMAGE_SIZE, forward.NUM_CHANNELS)) _, loss_value, step = sess.run([train_op, loss, global_step], feed_dict={x:reshaped_xs,y_:ys}) if i % 100 == 0: print("After %d training step(s), loss on training batch is %g." % (step, loss_value)) saver.save(sess, os.path.join(MODEL_SAVE_PATH, MODEL_NAME), global_step=global_step)
def test(): with tf.Graph().as_default() as g: initData() keep_prob = tf.placeholder(tf.float32) # dropout减轻过拟合问题 x = tf.placeholder(tf.float32, [None, 48 * 48]) y = tf.placeholder(tf.float32, [None, class_sum]) #调用前向传播 pred = forward.forward(x, True, REGULARIZER) #取测试数据 test_x = Face_data[train_num:train_num + test_num, :] test_y = Face_label[train_num:train_num + test_num, :] ''' train_x=Face_data [0: train_num, :] train_y= Face_label [0: train_num, :] ''' global_step = tf.Variable( 0, trainable=False) #定义变量global_step,并把它的属性设置为不可训练 # 评估模型 correct_pred = tf.equal(tf.argmax(pred, 1), tf.argmax(y, 1)) #比较预测值和标签值,argmax返回最大的那个数值所在的下标。 accuracy = tf.reduce_mean( tf.cast(correct_pred, "float")) #上面一句返回的是bool值,cast可以转换类型,此次将bool转换成float,即是0或1, #reduce_mean()可以计算一个列表中数据的均值 saver = tf.train.Saver(max_to_keep=1) #在会话中进行训练 while True: with tf.Session() as sess: #sess.run(tf.initialize_all_variables())#初始化 ckpt = tf.train.get_checkpoint_state( "./model") # 从"./model"中加载训练好的模型 if ckpt and ckpt.model_checkpoint_path: # 若ckpt和保存的模型在指定路径中存在,则将保存的神经网络模型加载到当前会话中 saver.restore(sess, ckpt.model_checkpoint_path) global_step = ckpt.model_checkpoint_path.split( '/')[-1].split('-')[-1] coord = tf.train.Coordinator() #4开启线程协调器 threads = tf.train.start_queue_runners(sess=sess, coord=coord) #5 acc = sess.run(accuracy, feed_dict={x: test_x, y: test_y}) print("after " + str(global_step) + " step(s) training " + "accuracy is " + "{:.3f}".format(acc)) coord.request_stop() coord.join(threads) else: print("No checkpoint file found") return time.sleep(TIME_INTERVAL)
def forback(P, pi, sigma, mu, X): T = X.shape[0] K = mu.shape[0] alpha, _ = forward(P, pi, sigma, mu, X) beta = backward(P, pi, sigma, mu, X) beta.shape gamma = np.zeros((T, K)) for i in range(T): for j in range(K): gamma[i][j] = alpha[i][j] * beta[i][j] for i in range(T): gamma[i, :], _ = normalize(gamma[i, :]) return gamma
def backward(): x = tf.placeholder(tf.float32, shape=(None, 2)) y_ = tf.placeholder(tf.float32, shape=(None, 1)) X, Y_, Y_c = generatedata.generateds() y = forward.forward(x, regular) global_step = tf.Variable(0, trainable=False) learning_rate = tf.train.exponential_decay(lr_base, global_step, 300 / batchsize, lr_decay, staircase=True) #定义损失函数 loss_mse = tf.reduce_mean(tf.square(y - y_)) loss_total = loss_mse + tf.add_n(tf.get_collection('losses')) #定义反向传播方法:包含正则化 train_step = tf.train.AdamOptimizer(learning_rate).minimize(loss_total) #train_step = tf.train.GradientDescentOptimizer(learning_rat).minimize(loss_total) #train_step = tf.train.MomentumOptimizer(learning_rat,0.9).minimize(loss_total) with tf.Session() as sess: init_op = tf.global_variables_initializer() sess.run(init_op) for i in range(STEPS): start = (i * batchsize) % 300 end = start + batchsize sess.run(train_step, feed_dict={ x: X[start:end], y_: Y_[start:end] }) if i % 2000 == 0: loss_v = sess.run(loss_total, feed_dict={x: X, y_: Y_}) print("经过 %d 轮训练, loss 值为: %f" % (i, loss_v)) xx, yy = np.mgrid[-3:3:.01, -3:3:.01] grid = np.c_[xx.ravel(), yy.ravel()] probs = sess.run(y, feed_dict={x: grid}) probs = probs.reshape(xx.shape) plt.scatter(X[:, 0], X[:, 1], c=np.squeeze(Y_c)) plt.contour(xx, yy, probs, levels=[.5]) plt.title("Build Neural Networks Examples") plt.show()
def backward(): x = tf.placeholder(tf.float32, shape=(None, 2)) y_ = tf.placeholder(tf.float32, shape=(None, 1)) X, Y_, Y_c = generateds.generateds() y = forward.forward(x, REGULARIZER) global_step = tf.Variable(0, trainable=False) learning_rate = tf.train.exponential_decay( LEARNING_RATE_BASE, global_step, 300 / BATCH_SIZE, #learning_rate_step,等于总样本数/BATCH_SIZE LEARNING_RATE_DECAY, staircase=True) #定义损失函数 loss_mse = tf.reduce_mean(tf.square(y - y_)) loss_total = loss_mse + tf.add_n(tf.get_collection('losses')) #定义反向传播方法:包含正则化 train_step = tf.train.AdamOptimizer(learning_rate).minimize(loss_total) with tf.Session() as sess: init_op = tf.global_variables_initializer() sess.run(init_op) for i in range(STEPS): start = (i * BATCH_SIZE) % 300 end = start + BATCH_SIZE sess.run(train_step, feed_dict={ x: X[start:end], y_: Y_[start:end] }) if i % 2000 == 0: loss_v = sess.run(loss_total, feed_dict={x: X, y_: Y_}) print("After %d steps, loss is %s" % (i, loss_v)) #np.mgrid函数构建坐标系的所有点的横纵坐标 xx, yy = np.mgrid[-3:3:.01, -3:3:.01] #np.c_函数将xx,yy匹配构成网格坐标系 grid = np.c_[xx.ravel(), yy.ravel()] probs = sess.run(y, feed_dict={x: grid}) probs = probs.reshape(xx.shape) #np.squeeze移除长度为1的轴,可以压缩维度 plt.scatter(X[:, 0], X[:, 1], c=np.squeeze(Y_c)) plt.contour(xx, yy, probs, levels=[.5]) plt.show()
def test(mnist): with tf.Graph().as_default() as g: #x y_占位 x = tf.placeholder(tf.float32, [ mnist.test.num_examples, forward.IMAGE_SIZE, forward.IMAGE_SIZE, forward.NUM_CHANNELS ]) y_ = tf.placeholder(tf.float32, [None, forward.OUTPUT_NODE]) #前向传播预测结果y y = forward.forward(x, False, None) ema = tf.train.ExponentialMovingAverage(backward.MOVING_AVERAGE_DECAY) ema_restore = ema.variables_to_restore() saver = tf.train.Saver(ema_restore) #计算正确率 correct_prediction = tf.equal(tf.argmax(y, 1), tf.argmax(y_, 1)) accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32)) while True: with tf.Session() as sess: #加载训练好的模型 ckpt = tf.train.get_checkpoint_state(backward.MODEL_SAVE_PATH) #如果已有ckpt则恢复 if ckpt and ckpt.model_checkpoint_path: #恢复会话 saver.restore(sess, ckpt.model_checkpoint_path) #恢复轮数 global_step = ckpt.model_checkpoint_path.split( '/')[-1].split('-')[-1] reshaped_x = np.reshape( mnist.test.images, (mnist.test.num_examples, forward.IMAGE_SIZE, forward.IMAGE_SIZE, forward.NUM_CHANNELS)) #计算准确率 accuracy_score = sess.run(accuracy, feed_dict={ x: reshaped_x, y_: mnist.test.labels }) print("After %s training step(s), test accuracy = %g" % (global_step, accuracy_score)) #如果没有模型 else: print('No checkpoint file found') #模型不存在 return time.sleep(TEST_INTERVAL_SECS)
def backward(): initData() keep_prob = tf.placeholder(tf.float32) # dropout减轻过拟合问题 x = tf.placeholder(tf.float32, [None, 48*48]) y = tf.placeholder(tf.float32, [None, class_sum]) #调用前向传播 pred = forward.forward(x,True,REGULARIZER) #取测试数据 train_x=Face_data [0: train_num, :] train_y= Face_label[0: train_num, :] global_step = tf.Variable(0, trainable = False) #定义变量global_step,并把它的属性设置为不可训练 # 评估模型 correct_pred = tf.equal(tf.argmax(pred,1), tf.argmax(y,1)) #比较预测值和标签值,argmax返回最大的那个数值所在的下标。 #1是表示返回一行中最大的元素的下标,y的一行有6个0和一个1 cross_entropy = -tf.reduce_sum(y*tf.log(pred)) train_step = tf.train.AdamOptimizer(1e-4).minimize(cross_entropy,global_step = global_step)#使用AdamOptimizer优化器,根据交叉熵进行反向传播训练 lossuracy = tf.reduce_mean(tf.cast(correct_pred, "float")) #上面一句返回的是bool值,cast可以转换类型,此次将bool转换成float,即是0或1, #reduce_mean()可以计算一个列表中数据的均值 saver = tf.train.Saver(max_to_keep=1) #在会话中进行训练 with tf.Session() as sess: sess.run(tf.initialize_all_variables())#初始化 ckpt = tf.train.get_checkpoint_state("./model") # 从"./model"中加载训练好的模型 if ckpt and ckpt.model_checkpoint_path: # 若ckpt和保存的模型在指定路径中存在,则将保存的神经网络模型加载到当前会话中 saver.restore(sess, ckpt.model_checkpoint_path) coord = tf.train.Coordinator() #4开启线程协调器 threads = tf.train.start_queue_runners(sess=sess, coord=coord) #5 for epoch in range(0, train_epoch): #训练train_epoch轮 Total_train_loss= 0 for train_batch in range (0, train_batch_num): batch_x,batch_y = batch_data(train_x, train_y, train_batch, train_num) # 优化操作 _,step=sess.run([train_step,global_step], feed_dict={x: batch_x, y: batch_y, keep_prob: dropout})#进行前向传播,与反向传播 if step % print_point == 0: # 计算损失 loss = sess.run(cross_entropy, feed_dict={x: batch_x, y: batch_y}) print("Epoch: "+str(epoch)+ " after "+str(step)+" step(s) training " +" loss is " + "{:.3f}".format(loss)) saver.save(sess, os.path.join(MODEL_SAVE_PATH, MODEL_NAME),global_step = global_step) # 保存当前模型 Total_train_loss= Total_train_loss+loss total_train_loss.append(Total_train_loss) coord.request_stop() coord.join(threads)
def test(): with tf.Graph().as_default() as g: x = tf.placeholder(tf.float32, [None, forward.INPUT_NODE]) y_ = tf.placeholder(tf.float32, [None, forward.OUTPUT_NODE]) y = forward.forward(x, None) #喂入对应的x,y数据,使用forward文件中的forward函数搭建网络 ema = tf.train.ExponentialMovingAverage(backward.MOVING_AVERAGE_DECAY) ema_restore = ema.variables_to_restore() saver = tf.train.Saver(ema_restore) #滑动平均 correct_prediction = tf.equal(tf.argmax(y, 1), tf.argmax(y_, 1)) accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32)) #正则化 img_batch, label_batch = generateds.get_tfrecord( TEST_NUM, isTrain=False) #从测试集中选择数据,数据没有被训练过,应该填False while (True): with tf.Session() as sess: ckpt = tf.train.get_checkpoint_state(backward.MODEL_SAVE_PATH) print(ckpt.model_checkpoint_path) if (ckpt and ckpt.model_ckeckpoint_path): saver.restore(sess, ckpt.model_checkpoint_path) #从ckpt.model_ckeckpoint_path中读取数据 global_step = ckpt.model_checkpoint_path.split( '/')[-1].split('-')[-1] coord = tf.train.Coordinator() #extre threads = tf.train.start_queue_runners(sess=sess, coord=coord) #extre xs, ys = sess.run([img_batch, label_batch]) #extre #before : accuracy_score = sess.run(accuracy, feed_dict={x: mnist.test.images, y_:mnist.test.labels}) accuracy_score = sess.run(accuracy, feed_dict={ x: xs, y: ys }) print("After %s training step(s), test accuracy = %g" % (global_step, accuracy_score)) coord.request_stop() #extre coord.join(threads) #extre关闭多线程 else: print("No checkpoint file found") return time.sleep(TEST_INTERVAL_SECS)
def backward(): x = tf.placeholder(tf.float32, shape=(None, 2)) y_ = tf.placeholder(tf.float32, shape=(None, 1)) X, Y_, Y_c = generateds.generateds() y = forward.forward(x, REGULARIZER) global_step = tf.Variable(0, trainable=False) lr = tf.train.exponential_decay(LR_BASE, global_step, 300 / BATCH_SIZE, LR_DECAY, staircase=True) loss_mse = tf.reduce_mean(tf.square(y - y_)) loss_total = loss_mse + tf.add_n(tf.get_collection('losses')) # 有正则化 train_step = tf.train.AdamOptimizer(lr).minimize(loss_total) with tf.Session() as sess: init_op = tf.global_variables_initializer() sess.run(init_op) for i in range(STEPS): start = (i * BATCH_SIZE) % 300 end = start + BATCH_SIZE sess.run(train_step, feed_dict={ x: X[start:end], y_: Y_[start:end] }) if i % 2000 == 0: loss_mse_v = sess.run(loss_mse, feed_dict={x: X, y_: Y_}) print('Step:%d , loss:%f ' % (i, loss_mse_v)) xx, yy = np.mgrid[-3:3:0.1, -3:3:0.1] grid = np.c_[xx.ravel(), yy.ravel()] probs = sess.run(y, feed_dict={x: grid}) probs = probs.reshape(xx.shape) # print "W1:\n",sess.run(W1) # print "b1:\n",sess.run(b1) # print "W2:\n",sess.run(W2) # print "b2:\n",sess.run(b2) plt.scatter(X[:, 0], X[:, 1], c=np.squeeze(Y_c)) plt.contour(xx, yy, probs, levels=[.5]) plt.show()
def backward(): x = tf.placeholder(tf.float32, shape=(None, 2)) y_ = tf.placeholder(tf.float32, shape=(None, 1)) X, Y_, Y_c = data.generate_dataset() y = forward.forward(x, REGULARIZER) global_step = tf.Variable(0, trainable=False) learning_rate = tf.train.exponential_decay(LEARNING_RATE_BASE, global_step, 300 / BATCH_SIZE, LEARNING_RATE_DECAY, staircase=True) loss_mse = tf.reduce_mean(tf.square(y - y_)) loss_total = loss_mse + tf.add_n(tf.get_collection('losses')) train_step = tf.train.AdamOptimizer(learning_rate).minimize( loss_total, global_step=global_step) with tf.Session() as sess: init_op = tf.global_variables_initializer() sess.run(init_op) for i in range(STEPS): start = (i * BATCH_SIZE) % 300 end = start + BATCH_SIZE sess.run(train_step, feed_dict={ x: X[start:end], y_: Y_[start:end] }) if 0 == i % 2000: loss_val = sess.run(loss_total, feed_dict={x: X, y_: Y_}) learning_rate_val = sess.run(learning_rate) fmt = "After {:05d} steps, loss is {:.09f}, learing rate is {:.09f}" print(fmt.format(i, loss_val, learning_rate_val)) # 在x和y轴,以步长为0.01,在-3到3之间生成二维坐标点 xx, yy = np.mgrid[-3:3:.01, -3:3:.01] # 将xx和yy拉直,合并为2列矩阵,得到网格坐标点集合 grid = np.c_[xx.ravel(), yy.ravel()] # 将网格坐标点输入神经网络,probs为输出 probs = sess.run(y, feed_dict={x: grid}) # probs的shape调整为xx的形式 probs = probs.reshape(xx.shape) plt.scatter(X[:, 0], X[:, 1], c=np.squeeze(Y_c)) plt.contour(xx, yy, probs, levels=[.5]) plt.show()
def backward(): x = tf.placeholder(tf.float32, shape=(None, 2)) y_ = tf.placeholder(tf.float32, shape=(None, 1)) X, Y_, Y_c = generateds.generateds() y = forward.forward(x, REGULARIZER) global_step = tf.Variable(0, trainable=False) learning_rate = tf.train.exponential_decay(LEARNING_RATE_BASE, global_step, 300 / BATCH_SIZE, LEARNING_RATE_DECAY, staircase=True) loss_mes = tf.reduce_mean(tf.square(y - y_)) loss_total = loss_mes + tf.add_n(tf.get_collection('losses')) train_step = tf.train.AdamOptimizer(learning_rate).minimize( loss_total, global_step=global_step) with tf.Session() as sess: init_op = tf.global_variables_initializer() sess.run(init_op) for i in range(STEPS): start = (i * BATCH_SIZE) % 300 end = start + BATCH_SIZE sess.run(train_step, feed_dict={ x: X[start:end], y_: Y_[start:end] }) if i % 2000 == 0: loss_v = sess.run(loss_total, feed_dict={x: X, y_: Y_}) learning_rate_v = sess.run(learning_rate) print('After {0} steps, loss is: {1}, learning_rate is: {2}'. format(i, loss_v, learning_rate_v)) xx, yy = np.mgrid[-3:3:0.01, -3:3:0.01] grid = np.c_[xx.ravel(), yy.ravel()] probs = sess.run(y, feed_dict={x: grid}) probs = probs.reshape(xx.shape) plt.scatter(X[:, 0], X[:, 1], c=np.squeeze(Y_c)) plt.contour(xx, yy, probs, levels=[0.5]) plt.show()
def index(): post_json = request.get_json(force=True) claim = str(post_json['claim']) print("claim", claim) text_response = forward.forward(claim) data = { "rebuttal": text_response, } response = app.response_class( response=json.dumps(data), status=200, mimetype='application/json' ) return response
def backward(mnist): x = tf.placeholder(tf.float32, [None, forward.INPUT_NODE]) y_ = tf.placeholder(tf.float32, [None, forward.OUTPUT_NODE]) y = forward.forward(x, REGULARIZER) global_step = tf.Variable(0, trainable=False) loss = loss_regularizer(y, y_) learning_rate = tf.train.exponential_decay(LEARNING_RATE_BASE, global_step, mnist.train.num_examples / BATCH_SIZE, LEARNING_RATE_DECAY, staircase=True) train_step = tf.train.GradientDescentOptimizer(learning_rate).minimize( loss, global_step=global_step) train_op = moving_average(global_step, train_step) saver = tf.train.Saver() img_batch, label_batch = generate_dataset.get_tfRecord(BATCH_SIZE, isTrain=True) with tf.Session() as sess: init_op = tf.global_variables_initializer() sess.run(init_op) #断点继续训练 ckpt = tf.train.get_checkpoint_state(MODEL_SAVE_PATH) if ckpt and ckpt.model_checkpoint_path: saver.restore(sess, ckpt.model_checkpoint_path) coord = tf.train.Coordinator() threads = tf.train.start_queue_runners(sess=sess, coord=coord) for i in range(STEPS): xs, ys = sess.run([img_batch, label_batch]) _, loss_value, step = sess.run([train_op, loss, global_step], feed_dict={ x: xs, y_: ys }) if i % 1000 == 0: print('after %d training steps, loss is: %g' % (step, loss_value)) saver.save(sess, os.path.join(MODEL_SAVE_PATH, MODEL_NAME), global_step=global_step) coord.request_stop() coord.join(threads)
def backward(): x = tf.placeholder(tf.float32, shape=(None, 2)) y_= tf.placeholder(tf.float32, shape=(None,1)) X, Y_, color = data_generate.generateData() y = forward.forward(x,REGULARIZER) global_step = tf.Variable(0,trainable=False) learn_rating = tf.train.exponential_decay(LEARN_RATEING_BASE ,global_step ,300/BATCH_SIZE ,LEARN_RATEING_DECAY ,staircase=True) #define loss function loss_mse = tf.reduce_mean(tf.square(y-y_)) loss_total = loss_mse + tf.add_n(tf.get_collection('losses')) #define backward train_step = tf.train.AdamOptimizer(learn_rating).minimize(loss_total) loss_array = [] with tf.Session() as sess: init_op = tf.global_variables_initializer() sess.run(init_op) for i in range(STEPS+1): start = (i*BATCH_SIZE)% 300 end = (start + BATCH_SIZE)%300 sess.run(train_step,feed_dict={x: X[start:end],y_: Y_[start:end]}) if i % 2000 == 0: loss_v = sess.run(loss_total,feed_dict={x:X,y_:Y_}) loss_array.append(loss_v) print("After %d steps, loss is %f"%(i, loss_v)) xx, yy = np.mgrid[-3:3:0.01, -3:3:0.01] grid = np.c_[xx.flatten(),yy.flatten()] probs = sess.run(y, feed_dict={x:grid}) probs = probs.reshape(xx.shape) plt.figure(1) plt.cla() plt.axis('equal') plt.scatter(X[:,0],X[:,1],c = color) plt.contour(xx, yy, probs,levels=[0.5]) plt.pause(0.001) plt.figure(2) plt.cla() plt.plot(loss_array) plt.pause(0.001)
def evaluate(mnist): with tf.Graph().as_default() as g: # 定义输入的格式 x = tf.placeholder(dtype=tf.float32, shape=[None, forward.INPUT_NODE], name='x-input') y_ = tf.placeholder(dtype=tf.float32, shape=[None, forward.OUTPUT_NODE], name='y-input') validate_feed = { x: mnist.validation.images, y_: mnist.validation.labels } ''' 直接通过封装好的函数来计算前向传播的结果,因为测试时并不关注正则化损失函数的值,所以这里用于计算正则化损失的函数被设置为None ''' y = forward.forward(x, None) # 使用前向传播的结果计算正确率,如果需要对未知的分类样例进行分类,那么使用tf.argmax(y,1)就可以得到输入样例的预测类别 correction_prediction = tf.equal(tf.argmax(y, 1), tf.argmax(y_, 1)) accuracy = tf.reduce_mean(tf.cast(correction_prediction, tf.float32)) ''' 通过变量重命名方式来加载模型,这样在前向传播的过程中就不需要调用求滑动平均的函数来获取平均值了 这样就可以共用forward.py中定义的前向传播过程 ''' variable_averages = tf.train.ExponentialMovingAverage( train.MOVING_AVERAGE_DECAY) variables_to_restore = variable_averages.variables_to_restore() saver = tf.train.Saver(variables_to_restore) # 每隔EVAL_INTERVAL_SECS秒调用一次计算正确率的过程以检测训练过程中正确率的变化 while True: with tf.Session() as sess: # tf.train.get_checkpoint_state函数会通过checkpoint文件自动找到目录中最新模型的文件名 ckpt = tf.train.get_checkpoint_state(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,validataion accuracy = %g" % (global_step, accuracy_score)) else: print("No checkpoint file found") return
def backward(): x = tf.placeholder(tf.float32,[ BATCH_SIZE, forward.IMAGE_SIZE, forward.IMAGE_SIZE, forward.NUM_CHANNELS]) y_ = tf.placeholder(tf.float32, \ [None, forward.OUTPUT_NODE]) y = forward.forward(x,True, REGULARIZER) global_step = tf.Variable(0, trainable=False) ce = tf.nn.sparse_softmax_cross_entropy_with_logits\ (logits=y, labels=tf.argmax(y_, 1)) cem = tf.reduce_mean(ce) loss = cem + tf.add_n(tf.get_collection('losses')) learning_rate = tf.train.exponential_decay( LEARNING_RATE_BASE, global_step, train_num_examples / BATCH_SIZE, LEARNING_RATE_DECAY, staircase=True) train_step = tf.train.GradientDescentOptimizer\ (learning_rate).minimize(loss, global_step=global_step) ema = tf.train.ExponentialMovingAverage\ (MOVING_AVERAGE_DECAY, global_step) ema_op = ema.apply(tf.trainable_variables()) with tf.control_dependencies([train_step, ema_op]): train_op = tf.no_op(name='train') saver = tf.train.Saver() img_batch, label_batch = generateds.get_tfrecord\ (BATCH_SIZE, isTrain=True)
def test(): #复现之前的计算图 with tf.Graph().as_default() as g: x = tf.placeholder(tf.float32, [None, 128,128,3]) y_ = tf.placeholder(tf.float32, [None, 6]) y = forward.forward(x, REGULARIZER) # 实例化具有滑动平均的saver对象从而在会话被加载时模型 # 中的所有参数被赋值为各自的滑动平均值,增强模型的稳定性,然后计算模型在 # 测试集上的准确率。 ema = tf.train.ExponentialMovingAverage(backward.MOVING_AVERAGE_DECAY) ema_restore = ema.variables_to_restore() saver = tf.train.Saver(ema_restore) correct_prediction = tf.equal(tf.argmax(y, 1), tf.argmax(y_, 1)) accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32)) #获得数据输入 img_batch, label_batch = generateds.readTFReccord(TFR_save_dir) # 在 with 结构中,加载指定路径下的 ckpt,若模型存在,则 # 加载出模型到当前对话,在测试数据集上进行准确率验证,并打印出当前轮数下 # 的准确率,若模型不存在,则打印出模型不存在的提示,从而 test()函数完成。 # 通过主函数 main(),加载指定路径下的测试数据集,并调用规定的 test 函数, # 进行模型在测试集上的准确率验证。 while True: with tf.Session() as sess: ckpt = tf.train.get_checkpoint_state(backward.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] coord = tf.train.Coordinator()#3 threads = tf.train.start_queue_runners(sess=sess, coord=coord)#4 xs, ys = sess.run([img_batch, label_batch]) accuracy_score = sess.run(accuracy, feed_dict={x: xs, y_: ys}) print("After %s training step(s), test accuracy = %g" % (global_step, accuracy_score)) coord.request_stop()#6 coord.join(threads)#7 else: print('No checkpoint file found') return time.sleep(TEST_INTERVAL_SECS)
def __init__(self, stylizer_arg): self.stylizer_arg = stylizer_arg self.content = tf.placeholder(tf.float32, [1, None, None, 3]) # 图片输入定义 self.weight = tf.placeholder(tf.float32, [1, stylizer_arg.LABELS_NUMS]) # 风格权重向量,用于存储用户选择的风格 self.target = forward(self.content, self.weight) # 定义将要生成的图片 self.img_path = stylizer_arg.PATH_IMG self.img = None self.label_list = None self.input_weight = None self.img25 = None self.img25_4 = None self.sess = tf.Session() # 定义一个sess self.sess.run(tf.global_variables_initializer()) # 变量初始化 saver = tf.train.Saver() # 定义模型saver ckpt = tf.train.get_checkpoint_state(stylizer_arg.PATH_MODEL) # 从模型存储路径中获取模型 if ckpt and ckpt.model_checkpoint_path: # 从检查点中恢复模型 saver.restore(self.sess, ckpt.model_checkpoint_path)
def backward(): x = tf.placeholder(tf.float32, shape=(None, 2)) y_ = tf.placeholder(tf.float32, shape=(None, 1)) X, Y_, Y_c = generates.generates() y = forward.forward(x, REGULARIZER) global_step = tf.Variable(0, trainable = False) learning_rate = tf.train.exponential_decay( LEARNING_RATE_BASE, global_step, 300/BATCH_SIZE, LEARNINT_RATE_DECAY, staircase=True) loss_mse = tf.reduce_mean(tf.square(y-y_)) loss_total = loss_mse + tf.add_n(tf.get_collection('losses')) train_step = tf.train.AdamOptimizer(learning_rate).minimize(loss_total) with tf.Session() as sess: init_op = tf.global_variables_initializer() sess.run(init_op) STEPS = 40000 for i in range(STEPS): start = (i*BATCH_SIZE) % 300 end = start + BATCH_SIZE sess.run(train_step, feed_dict={x:X[start:end], y_:Y_[start:end]}) if i % 2000 == 0: loss_v = sess.run(loss_mse, feed_dict = {x:X, y_:Y_}) print("After %d steps, loss is: %f"%(i, loss_v)) xx, yy = np.mgrid[-3:3:.01, -3:3:01] grid = np.c_[xx.ravel(), yy.ravel()] probs = sess.run(y, feed_dict={x:grid}) probs = probs.reshape(xx.shape) plt.scatter(X[:,0], X[:,1],c=np.squeeze(Y_c)) plt.contour(xx, yy, probs, levels=[.5]) plt.show()
def restore_model(testPicArr): with tf.Graph().as_default() as tg: x = tf.placeholder(tf.float32, [None, forward.INPUT_NODE]) y = forward.forward(x, None) preValue = tf.argmax(y, 1) variable_averages = tf.train.ExponentialMovingAverage(backward.MOVING_AVERAGE_DECAY) variables_to_restore = variable_averages.variables_to_restore() saver = tf.train.Saver(variables_to_restore) with tf.Session() as sess: ckpt = tf.train.get_checkpoint_state(backward.MODEL_SAVE_PATH) if ckpt and ckpt.model_checkpoint_path: saver.restore(sess, ckpt.model_checkpoint_path) preValue = sess.run(preValue, feed_dict={x:testPicArr}) return preValue else: print("No checkpoint file found") return -1
def predict(inputLayer, weight1, weight2): print("Starting prediction.") error = np.zeros(1) for index in range(1): # run the forward function hiddenLayer, outputLayer = forward(inputLayer, weight1, weight2) print("Hidden layer:") print(hiddenLayer) print("Output layer:") print(outputLayer) # save the error error[index] = 1/2 * (outputLayer - target)**2 print("Error: {:.8f}.".format(error[index])) print("Prediction complete.")
def test(mnist): with tf.Graph().as_default() as g: x = tf.placeholder(tf.float32, [None, forward.INPUT_NODE]) y_ = tf.placeholder(tf.float32, [None, forward.OUTPUT_NODE]) y = forward.forward(x, None) ema = tf.train.ExponentialMovingAverage(backward.MOVING_AVERAGE_DECAY) ema_restore = ema.variables_to_restore() saver = tf.train.Saver(ema_restore) correct_prediction = tf.equal(tf.argmax(y, 1), tf.argmax(y_, 1)) accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32)) img_batch, label_batch = generateds.get_tfrecord(TEST_NUM, isTrain=False) while True: with tf.Session() as sess: ckpt = tf.train.get_checkpoint_state(backward.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] coord = tf.train.Coordinator() threads = tf.train.start_queue_runners(sess=sess, coord=coord) xs, ys = sess.run([img_batch, label_batch]) accuracy_score = sess.run(accuracy, feed_dict={x:mnist.test.images, y_:mnist.test.labels}) print("After %s training step(s), test accuracy = %g" % (global_step, accuracy_score)) coord.request_stop() coord.join(threads) else: print("No checkpoint file found") return time.sleep(TEST_INTERVAL_SECS)
def predict(path, weightPath1, weightPath2, method): predictionPath = "predictions.txt" predictions = [] limit = 500 # load weight1 = np.load(weightPath1) weight2 = np.load(weightPath2) method = method.lower() if(method == 'fasta'): proteins = fileUtils.readFasta(path) for proteinId in range(len(proteins)): sequences = sequenceUtils.openReadingFrames(proteins[proteinId]) for pos in range(len(sequences)): # lav sekvens om til binær inputLayer = sequenceUtils.createInputLayer(sequences[pos]) # forward outputLayer = forward(inputLayer, weight1, weight2)[1] outputLayer = logTransform.invTransform(outputLayer) if(outputLayer <= limit): # plus one, since both are zero indexed predictions.append([proteinId + 1, pos + 1]) np.savetxt(predictionPath, np.array(predictions), fmt = '%d', delimiter = '\t') print("There is {} predicted epitopes.".format(len(predictions)))
for proteinId in range(len(hivProteins)): allSequences[proteinId] = sequenceUtils.openReadingFrames(hivProteins[proteinId]) for pos in range(len(allSequences[proteinId])): ## machine # save sequence machine0.append(allSequences[proteinId][pos]) # lav sekvens om til binær inputLayer = sequenceUtils.createInputLayer(allSequences[proteinId][pos]) # forward outputLayer = forward(inputLayer, weight1, weight2)[1] outputLayer = logTransform.invTransform(outputLayer) # save prediction machine1.append(outputLayer) # save epitope if(outputLayer <= limit): # plus one, since both are zero indexed machine[0].append(allSequences[proteinId][pos]) machine[1].append(proteinId + 1) machine[2].append(pos + 1) ## epitopes if(np.any(allSequences[proteinId][pos] == np.array(hivEpitopes))):
def workflow(q, kb, depth): best = etcetera.nbest(q.given(), kb, depth, 1)[0] return(forward.graph(best, forward.forward(best, kb), targets=q.given()))
def xbestproof(q, kb, depth, x): xbest = etcetera.nbest(q.given(), kb, depth, x + 1)[x] return(forward.graph(xbest, forward.forward(xbest, kb), targets=q.given()))
def entailedlist(obs, nbest, kb): return [[pair[0] for pair in forward.forward(n, kb)] for n in nbest]
def FOM(params): [sigmax, sigmay] = params result = forward.forward(spotlist,sigmax,sigmay) return result
# Load files inlines = args.infile.readlines() intext = "".join(inlines) kb, obs = parse.definite_clauses(parse.parse(intext)) if args.kb: kblines = args.kb.readlines() kbtext = "".join(kblines) kbkb, kbobs = parse.definite_clauses(parse.parse(kbtext)) kb.extend(kbkb) # Handle forward if args.forward: entailed = forward.forward(obs, kb) if args.graph: print(forward.graph(obs, entailed), file=args.outfile) else: for e in entailed: print(parse.display(e[0]), file=args.outfile) sys.exit() # Handle abduction if args.all: solutions = etcetera.etcAbduction(obs, kb, args.depth) else: solutions = etcetera.nbest(obs, kb, args.depth, args.nbest) if args.graph:
def train(path, weightPath1, weightPath2, hiddenNodes, epochs, learningRate, proceed): # read sequences and their measured binding affinities allSequences, allTargets = fileUtils.readHLA(path) # log transform the data to fit between 0 and 1 allTargets = logTransform.transform(allTargets) # divide the data into training set, validation set and evaluation set numOfSequences = len(allSequences) indexes = np.arange(numOfSequences) np.random.shuffle(indexes) numOfTrain = (int) (numOfSequences * 0.7) # 70 % is for training trainSequence = allSequences[indexes[0:numOfTrain]] trainTarget = allTargets[indexes[0:numOfTrain]] numOfVal = (int) (numOfSequences * 0.2) # 20 % is for vaidation valSequence = allSequences[indexes[numOfTrain:(numOfTrain + numOfVal)]] valTarget = allTargets[indexes[numOfTrain:(numOfTrain + numOfVal)]] evalSequence = allSequences[indexes[(numOfTrain + numOfVal):numOfSequences]] evalTarget = allTargets[indexes[(numOfTrain + numOfVal):numOfSequences]] evalPrediction = np.zeros(len(evalSequence)) trainError = np.zeros(epochs) valError = np.zeros(epochs) # længden af sekvensbiderne og antallet er mulige aminosyrer. Der er 20 normale. mer = 9 numOfAminoAcids = 20 # create weight matrix with random values or load the files if(proceed): weight1 = np.load(weightPath1) weight2 = np.load(weightPath2) else: weight1 = weight(hiddenNodes, numOfAminoAcids * mer + 1) # plus 1 for bias weight2 = weight(1, hiddenNodes + 1) # plus 1 for bias bestWeight1 = weight1 bestWeight2 = weight2 bestError = 999 # just a large number so any validation will be better bestEpoch = 0 print("Starting training and validation.") for epoch in range(epochs): # train on training set # make scrampled order of sequences indexes = np.arange(numOfTrain) np.random.shuffle(indexes) error = np.zeros(numOfTrain) for index in indexes: # convert peptide sequence to quasi-binary inputLayer = sequenceUtils.createInputLayer(trainSequence[index]) # run the forward function hiddenLayer, outputLayer = forward(inputLayer, weight1, weight2) # save the error error[index] = 1/2 * (outputLayer - trainTarget[index])**2 errorDelta = outputLayer - trainTarget[index] # backpropagation outputDelta = backpropagation.backward(outputLayer, 1, errorDelta) weight2 = backpropagation.updateWeight(hiddenLayer, weight2, outputDelta, learningRate) hiddenDelta = backpropagation.backward(hiddenLayer, weight2, outputDelta) # bias is not a part of calculating the weights for the input hiddenDelta = hiddenDelta[0,0:hiddenNodes] weight1 = backpropagation.updateWeight(inputLayer, weight1, hiddenDelta, learningRate) trainError[epoch] = error.mean() # validation error = np.zeros(numOfVal) for index in range(numOfVal): # convert peptide sequence to quasi-binary inputLayer = sequenceUtils.createInputLayer(valSequence[index]) # run the forward function hiddenLayer, outputLayer = forward(inputLayer, weight1, weight2) # save the error error[index] = 1/2 * (outputLayer - valTarget[index])**2 valError[epoch] = error.mean() # find the best weight matrices so far if(valError[epoch] < bestError): bestWeight1 = weight1 bestWeight2 = weight2 bestError = valError[epoch] bestEpoch = epoch if(epoch % 10 == 0): percent = (int) (epoch/epochs*100) print("Training error: {:.8f}. Validation error: {:.8f}. {:2}% complete." .format(trainError[epoch], valError[epoch], percent)) print("Training and validation complete.") # plot error pyplot.plot(trainError, label = "Training set") pyplot.plot(valError, label = "Validation set") pyplot.legend(bbox_to_anchor=(1.05, 1), loc=2, borderaxespad=0) pyplot.xlabel("epoch") pyplot.ylabel("error") pyplot.title("Validation") pyplot.savefig('validation.png', bbox_inches='tight') pyplot.show() # save the best weight matrices np.save(weightPath1, bestWeight1) np.save(weightPath2, bestWeight2) print("The minimum error of the validation set is at epoch {}. The validation error is {}." .format(bestEpoch, bestError)) #evaluation print("Predicting on evaluation set.") for index in range(len(evalSequence)): # convert peptide sequence to quasi-binary inputLayer = sequenceUtils.createInputLayer(evalSequence[index]) # run the forward function hiddenLayer, outputLayer = forward(inputLayer, bestWeight1, bestWeight2) evalPrediction[index] = outputLayer # plot comparison of prediction and target for evaluation set pyplot.plot(evalTarget, evalPrediction, '.') pyplot.xlabel("target") pyplot.ylabel("prediction") pyplot.title("Evaluation") pyplot.savefig('evaluationLog.png', bbox_inches='tight') pyplot.show() # how correlated is it? corr = np.corrcoef(evalTarget, evalPrediction)[1,0] print("The Pearson correlation coefficient is {}.".format(corr)) # plot comparison again, now inverse log transfomed back but with a logarithmic scale evalPrediction = logTransform.invTransform(evalPrediction) evalTarget = logTransform.invTransform(evalTarget) pyplot.axes().set_xscale('log') pyplot.axes().set_yscale('log') pyplot.plot(evalTarget, evalPrediction, '.') pyplot.xlabel("target") pyplot.ylabel("prediction") pyplot.title("Evaluation") pyplot.savefig('evaluation.png', bbox_inches='tight') pyplot.show()