ml for price forecasting rstudio code example
Example: ml for price forecasting rstudio
# Make Sessionnet = tf.Session()# Run initializernet.run(tf.global_variables_initializer())# Setup interactive plotplt.ion()fig = plt.figure()ax1 = fig.add_subplot(111)line1, = ax1.plot(y_test)line2, = ax1.plot(y_test*0.5)plt.show()# Number of epochs and batch sizeepochs = 10batch_size = 256for e in range(epochs): # Shuffle training data shuffle_indices = np.random.permutation(np.arange(len(y_train))) X_train = X_train[shuffle_indices] y_train = y_train[shuffle_indices] # Minibatch training for i in range(0, len(y_train) // batch_size): start = i * batch_size batch_x = X_train[start:start + batch_size] batch_y = y_train[start:start + batch_size] # Run optimizer with batch net.run(opt, feed_dict={X: batch_x, Y: batch_y}) # Show progress if np.mod(i, 5) == 0: # Prediction pred = net.run(out, feed_dict={X: X_test}) line2.set_ydata(pred) plt.title('Epoch ' + str(e) + ', Batch ' + str(i)) file_name = 'img/epoch_' + str(e) + '_batch_' + str(i) + '.jpg' plt.savefig(file_name) plt.pause(0.01)# Print final MSE after Trainingmse_final = net.run(mse, feed_dict={X: X_test, Y: y_test})print(mse_final)