import numpy as np import tensorflow as tf from flask import Flask, jsonify, render_template, request import time from mnist import model X1 = tf.placeholder("float", [None, 784]) X2 = tf.placeholder(tf.float32, [None, 28, 28, 1]) sess = tf.Session() # restore trained data with tf.variable_scope("regression"): Y1, variables = model.regression(X1) saver = tf.train.Saver(variables) saver.restore(sess, "mnist/data/regression.ckpt") print("Regression model restored.") with tf.variable_scope("convolutional"): pkeep = tf.placeholder(tf.float32) Y2, Ylogits, variables = model.convolutional(X2, pkeep) saver = tf.train.Saver(variables) saver.restore(sess, "mnist/data/convolutional.ckpt") print("Convolutional model restored.") def regression(input): return sess.run(Y1, feed_dict={X1: input}).flatten().tolist() def convolutional(input):
import numpy as np import tensorflow as tf from flask import Flask, jsonify, render_template, request from sklearn.externals import joblib from LinearRegression import my_model from mnist import model x = tf.placeholder("float", [None, 784]) x1 = tf.placeholder(tf.float32) sess = tf.Session() # restore trained data with tf.variable_scope("regression"): y1, variables = model.regression(x) saver = tf.train.Saver(variables) saver.restore(sess, "mnist/data/regression.ckpt") with tf.variable_scope("convolutional"): keep_prob = tf.placeholder("float") y2, variables = model.convolutional(x, keep_prob) saver = tf.train.Saver(variables) saver.restore(sess, "mnist/data/convolutional.ckpt") with tf.variable_scope("my_model"): y3, variables = my_model.regr(x1) saver = tf.train.Saver(variables) saver.restore(sess, "LinearRegression/data/linear_regression.ckpt") decision_tree = joblib.load('DecisionTree/data/parsing_tree.pkl') knn = joblib.load('KNN/data/knn.pkl')
import tensorflow as tf from db import DataStore from mnist import model db = DataStore() x = tf.placeholder("float", [None, 784]) sess = tf.Session() # restore trained data with tf.variable_scope("perceptron"): y_percep, perceptron_variables = model.multilayer_perceptron(x) with tf.variable_scope("regression"): y_reg, regression_variables = model.regression(x) with tf.variable_scope("convolutional"): keep_prob = tf.placeholder(tf.float32) y_conv, conv_variables = model.convolutional(x, keep_prob) with tf.variable_scope("rnn"): y_rnn, _ = model.rnn_network(x) rnn_variables = tf.get_collection(tf.GraphKeys.VARIABLES, scope='rnn') saver = tf.train.Saver(conv_variables + perceptron_variables + regression_variables + rnn_variables) saver.restore(sess, "mnist/data/mnist.ckpt") def regression(input):
import numpy as np import tensorflow as tf from flask import Flask, jsonify, render_template, request from mnist import model x = tf.placeholder("float", [None, 784]) sess = tf.Session() # restore trained data with tf.variable_scope("regression"): y1, variables = model.regression(x) saver = tf.train.Saver(variables) saver.restore(sess, "mnist/data/regression.ckpt") with tf.variable_scope("convolutional"): keep_prob = tf.placeholder("float") y2, variables = model.convolutional(x, keep_prob) saver = tf.train.Saver(variables) saver.restore(sess, "mnist/data/convolutional.ckpt") def regression(input): return sess.run(y1, feed_dict={x: input}).flatten().tolist() def convolutional(input): return sess.run(y2, feed_dict={x: input, keep_prob: 1.0}).flatten().tolist()
import numpy as np import tensorflow as tf from flask import Flask, jsonify, render_template, request from mnist import model x = tf.placeholder("float", [None, 784]) sess = tf.Session() # restore trained data with tf.variable_scope("regression"): logits1, variables1 = model.regression(x) y1 = tf.nn.softmax(logits1) saver = tf.train.Saver(variables1) saver.restore(sess, "mnist/data/regression.ckpt") with tf.variable_scope("convolutional"): keep_prob = tf.placeholder("float") logits2, variables2 = model.convolutional(x, keep_prob) y2 = tf.nn.softmax(logits2) saver = tf.train.Saver(variables2) saver.restore(sess, "mnist/data/convolutional.ckpt") def regression(input): return sess.run(y1, feed_dict={x: input}).flatten().tolist() def convolutional(input):
#!/usr/bin/python3 import os from mnist import input_data import tensorflow as tf from mnist.model import regression mnist = input_data.read_data_sets('MNIST_data', one_hot=True) # create a model with tf.variable_scope('regression'): x = tf.placeholder(tf.float32, [None, 784]) y, variables = regression(x) pass # train y_r = tf.placeholder('float', [None, 10]) # 真实值 cross_entropy = -tf.reduce_sum(y_r * tf.log(y)) # 预测值与真实值的交叉熵 train_step = tf.train.GradientDescentOptimizer(0.01).minimize( cross_entropy) # 使用梯度下降优化器最小化交叉熵 correct_prediction = tf.equal(tf.argmax(y, 1), tf.arg_max(y_r, 1)) # 比较预测值和真实值是否一致 accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32)) # 统计预测正确的个数,取均值得到准确率 saver = tf.train.Saver(variables) with tf.Session() as sess: sess.run(tf.global_variables_initializer()) for _ in range(1000): batch_xs, batch_ys = mnist.train.next_batch(
import numpy as np import tensorflow as tf from flask import Flask, jsonify, render_template, request import json from mnist import model # model.py x = tf.placeholder('float', [None, 784]) # 输入 sess = tf.Session() with tf.variable_scope('regression'): y1, variables = model.regression(x) # 作为参数???????怎么传的,基础 saver = tf.train.Saver(variables) saver.restore(sess, "mnist/data/regression.ckpt")# 拿数据 with tf.variable_scope("convolutional"): keep_prob = tf.placeholder('float') y2, variables = model.convolutional(x, keep_prob) saver = tf.train.Saver(variables) saver.restore(sess, "mnist/data/convolutional.ckpt") def regression(input):# 输入 return sess.run(y1, feed_dict={x: input}).flatten().tolist() def convolutional(input):# 输入 return sess.run(y2, feed_dict={x: input, keep_prob: 1.0}).flatten().tolist() app = Flask(__name__) # 定义flask