python - How to predict using trained Tensorflow model -
i have created , trained neural network able input test points , see results (rather using eval function).
the model runs fine , cost reduces every epoch, want add line @ end pass input coordinates , have tell me predicted transformed coordinates.
import tensorflow tf import numpy np def coordinate_transform(size, angle): input = np.random.rand(size, 2) output = np.zeros((size, 2)) noise = 0.05*(np.add(np.random.rand(size) * 2, -1)) theta = np.add(np.add(np.arctan(input[:,1] / input[:,0]) , angle) , noise) radii = np.sqrt(np.square(input[:,0]) + np.square(input[:,1])) output[:,0] = np.multiply(radii, np.cos(theta)) output[:,1] = np.multiply(radii, np.sin(theta)) return input, output #data input, output = coordinate_transform(2000, np.pi/2) train_in = input[:1000] train_out = output[:1000] test_in = input[1000:] test_out = output[1000:] # parameters learning_rate = 0.001 training_epochs = 15 batch_size = 1 display_step = 1 # network parameters n_hidden_1 = 100 # 1st layer number of features n_input = 2 # [x,y] n_classes = 2 # output x,y coords # tf graph input x = tf.placeholder("float", [1,n_input]) y = tf.placeholder("float", [1, n_input]) # create model def multilayer_perceptron(x, weights, biases): # hidden layer relu activation layer_1 = tf.add(tf.matmul(x, weights['h1']), biases['b1']) layer_1 = tf.nn.relu(layer_1) # output layer linear activation out_layer = tf.matmul(layer_1, weights['out']) + biases['out'] return out_layer # store layers weight & bias weights = { 'h1': tf.variable(tf.random_normal([n_input, n_hidden_1])), 'out': tf.variable(tf.random_normal([n_hidden_1, n_classes])) } biases = { 'b1': tf.variable(tf.random_normal([n_hidden_1])), 'out': tf.variable(tf.random_normal([n_classes])) } # construct model pred = multilayer_perceptron(x, weights, biases) # define loss , optimizer #cost = tf.losses.mean_squared_error(0, (tf.slice(pred, 0, 1) - x)**2 + (tf.slice(pred, 1, 1) - y)**2) cost = tf.losses.mean_squared_error(y, pred) optimizer = tf.train.adamoptimizer(learning_rate=learning_rate) optimizer = optimizer.minimize(cost) # initializing variables #init = tf.global_variables_initializer() init = tf.initialize_all_variables() # launch graph tf.session() sess: sess.run(init) # training cycle epoch in range(training_epochs): avg_cost = 0. total_batch = 1000#int(len(train_in)/batch_size) # loop on batches in range(total_batch): batch_x = train_in[i].reshape((1,2)) batch_y = train_out[i].reshape((1,2)) #print(batch_x.shape) #print(batch_y.shape) #print(batch_y, batch_x) # run optimization op (backprop) , cost op (to loss value) _, c = sess.run([optimizer, cost], feed_dict={x: batch_x, y: batch_y}) # compute average loss avg_cost += c / total_batch # display logs per epoch step if epoch % display_step == 0: print ("epoch:", '%04d' % (epoch+1), "cost=", \ "{:.9f}".format(avg_cost)) print("optimization finished!") # test model correct_prediction = tf.equal(tf.argmax(pred, 1), tf.argmax(y, 1)) # calculate accuracy accuracy = tf.reduce_mean(tf.cast(correct_prediction, "float")) #make predictions
well 'pred' op actual outcome (as it's used compare y when calculating loss), following should trick:
print(sess.run([pred], feed_dict={x: _input_goes_here_ }) obviously _input_goes_here_ need replaced actual input.
Comments
Post a Comment