def __init__(self, params, source_alphabet_size, embedding_size, hidden_units, stack_embedding_size): input_size = source_alphabet_size + 2 output_size = source_alphabet_size + 1 self.stack_embedding_size = stack_embedding_size self.input_embeddings = params.add_lookup_parameters( (input_size, embedding_size), name='input-embeddings') self.output_embeddings = params.add_lookup_parameters( (output_size, embedding_size), name='output-embeddings') self.controller = dy.CoupledLSTMBuilder( 1, embedding_size + stack_embedding_size, hidden_units, params) # Intentionally set the gain for the sigmoid layers low, since this # seems to work better gain = 0.5 self.pop_strength_layer = add_layer( params, hidden_units, 1, sigmoid, weights_initializer=dy.GlorotInitializer(False, gain=gain), # Initialize the pop bias to -1 to allow information to propagate # through the stack bias_initializer=dy.ConstInitializer(-1.0), name='pop-strength') self.push_strength_layer = add_layer( params, hidden_units, 1, sigmoid, weights_initializer=dy.GlorotInitializer(False, gain=gain), bias_initializer=dy.GlorotInitializer(False, gain=gain), name='push-strength') self.push_value_layer = add_layer( params, hidden_units, stack_embedding_size, tanh, name='push-value') self.output_layer = combine_layers([ add_layer(params, hidden_units, hidden_units, tanh, name='output'), # This adds an extra affine layer between the tanh and the softmax add_layer(params, hidden_units, output_size, linear, name='softmax') ])
@author: 31214 """ import tensorflow.compat.v1 as tf tf.disable_v2_behavior() import matplotlib.pyplot as plt import numpy as np from layer import add_layer, input_BN tf.set_random_seed(1) np.random.seed(1) x_data = np.linspace(0, 40, 1) y_data = np.linspace(0, 40, 1) x = tf.placeholder(tf.float32, [None, 1]) y = tf.placeholder(tf.float32, [None, 1]) l1 = add_layer(input_BN(x), 1, 3, activation_function=tf.nn.relu, norm=True) prediction = add_layer(l1, 3, 1, norm=True) loss = tf.losses.mean_squared_error(y, prediction) train = tf.train.AdamOptimizer(0.1).minimize(loss) sess = tf.Session() sess.run(tf.global_variables_initializer()) # initialize var in graph fig = plt.figure() ax = fig.add_subplot(1, 1, 1) ax.scatter(x_data, y_data) plt.ion() plt.show() for i in range(1000): # training
from layer import add_layer # define the x_data and y_data and noise variables # x_data = tf.linspace(-1., 1., 300)[:, np.newaxis] x_data = np.linspace(-1, 1, 300, dtype=np.float32)[:, np.newaxis] noise = np.random.normal(0, 0.05, x_data.shape) y_data = np.square(x_data) + noise - 0.5 with tf.name_scope('inputs'): # define the placeholder variables, make it easy to change the inputs_data xs = tf.placeholder(tf.float32, [None, 1], name='x_input') ys = tf.placeholder(tf.float32, [None, 1], name='y_input') # add the hiden layer to NN l1 = add_layer(xs, 1, 10, n_layer=1, activation_function=tf.nn.relu) # add the output layer to NN prediction = add_layer(l1, 10, 1, n_layer=2, activation_function=None) with tf.name_scope('loss'): # define the loss function loss = tf.reduce_mean(tf.square(ys - prediction)) tf.summary.scalar('loss', loss) with tf.name_scope('train'): # define the train_step(A Gradient Descent Optimizer) to minimize the loss function # optimizer = tf.train.GradientDescentOptimizer(0.5) # train_step = optimizer.minimize(loss) train_step = tf.train.GradientDescentOptimizer(0.2).minimize(loss) # define the initializer for all variables
plt.title("initial signal and process signal") plt.xlabel("x axis:") plt.ylabel("y axis:") plt.scatter(x_data, signal) plt.show() return signal # create study data x_data = np.linspace(-2, 2, 900)[:, np.newaxis] sig = 2 * np.sin(10 * x_data) + np.random.normal(0, 0.5, x_data.shape) tf_x = tf.placeholder(tf.float32, [None, 1]) tf_y = tf.placeholder(tf.float32, [None, 1]) #create neuron network with BN l1 = add_layer(input_BN(tf_x), 1, 5, activation_function=tf.nn.relu, norm=True) l2 = add_layer(l1, 5, 7, activation_function=tf.nn.relu, norm=True) l3 = add_layer(l2, 7, 9, activation_function=tf.nn.relu, norm=True) l4 = add_layer(l3, 9, 7, activation_function=tf.nn.relu, norm=True) l5 = add_layer(l4, 7, 5, activation_function=tf.nn.relu, norm=True) prediction = add_layer(l5, 5, 1, norm=True) #prediction = sh.saleh_model(prediction) #loss = tf.reduce_mean(tf.reduce_sum(tf.square(tf_y-prediction), reduction_indices=[1])) loss = tf.losses.mean_squared_error(tf_y, prediction) train = tf.train.AdamOptimizer(0.1).minimize(loss) saver = tf.train.Saver() # define a saver for saving and restoring sess = tf.Session() sess.run(tf.global_variables_initializer()) # initialize var in graph #saver.restore(sess, './datak') fig = plt.figure()
from tensorflow.examples.tutorials.mnist import input_data # training with GPU # tf.device('/gpu:1') # this is our mnist_data, if you havn't MNIST_data, it'll download the data, else it'll load data. mnist = input_data.read_data_sets('MNIST_data', one_hot=True) # define placeholder for inputs to network xs = tf.placeholder(tf.float32, [None, 784]) # 28 * 28 ys = tf.placeholder(tf.float32, [None, 10]) # 10 numbers # add hiden layer # l1 = add_layer(xs, 784, 10, 'hidden_layer', activation_function=tf.nn.tanh) # add output layer prediction = add_layer(xs, 784, 10, 'output_layer', activation_function=tf.nn.softmax) # the error between prediction and real data cross_entropy = tf.reduce_mean(-tf.reduce_sum(ys * tf.log(prediction), reduction_indices=[1])) # cross_entropy = -tf.reduce_sum(ys * tf.log(prediction)) train_step = tf.train.GradientDescentOptimizer(0.05).minimize(cross_entropy) sess = tf.Session() sess.run(tf.global_variables_initializer()) for i in range(1001): batch_xs, batch_ys = mnist.train.next_batch(100) sess.run(train_step, feed_dict={xs: batch_xs, ys: batch_ys}) if i % 50 == 0: