<jupyter_start><jupyter_text>Artificial Neural Network (ANN) Deep Neural Network (DNN) Regressor3 layers: 1) Input 2) Hidden and 3) Outputfeature: Input data to the network(features)label: output from the network (labels)loss function: Metric used to estimate the performance of the learning phaseoptimizer: Improve the learning by updating the knowledge in the network<jupyter_code>import numpy as np
import matplotlib.pyplot as plt
import pandas as pd

import warnings
warnings.filterwarnings("ignore")

# fix_yahoo_finance is used to fetch data 
import fix_yahoo_finance as yf
yf.pdr_override()<jupyter_output><empty_output><jupyter_text>(1) Import the data<jupyter_code># input
symbol = 'AMD'
start = '2014-01-01'
end = '2018-08-27'

# Read data 
dataset = yf.download(symbol,start,end)

# View Columns
dataset.head()
# Create more data
dataset['Increase_Decrease'] = np.where(dataset['Volume'].shift(-1) > dataset['Volume'],1,0)
dataset['Buy_Sell_on_Open'] = np.where(dataset['Open'].shift(-1) > dataset['Open'],1,-1)
dataset['Buy_Sell'] = np.where(dataset['Adj Close'].shift(-1) > dataset['Adj Close'],1,-1)
dataset['Returns'] = dataset['Adj Close'].pct_change()
dataset = dataset.dropna()
dataset.head()
dataset.shape
X = dataset.drop(['Adj Close', 'Close'], axis=1)  
y = dataset['Adj Close'] 
from sklearn.model_selection import train_test_split

X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
y_train  = y_train.astype(int)
y_test  = y_test.astype(int)
batch_size =len(X_train)

print(X_train.shape, y_train.shape,y_test.shape)<jupyter_output>(936, 8) (936,) (235,)<jupyter_text>(2) Transform the data<jupyter_code>## rescale
from sklearn.preprocessing import MinMaxScaler
scaler = MinMaxScaler()
# Train
X_train_scaled = scaler.fit_transform(X_train.astype(np.float64))
# test
X_test_scaled = scaler.fit_transform(X_test.astype(np.float64))
print(X_train_scaled.shape, X_test_scaled.shape)<jupyter_output>(936, 8) (235, 8)<jupyter_text>(3) Construct the tensor<jupyter_code>import tensorflow as tf

feature_columns = [tf.feature_column.numeric_column('x', shape=X_train_scaled.shape[1:])]<jupyter_output><empty_output><jupyter_text>(4) Build the model<jupyter_code>estimator = tf.estimator.DNNRegressor(
    feature_columns=feature_columns,
    hidden_units=[300, 100])<jupyter_output>INFO:tensorflow:Using default config.
WARNING:tensorflow:Using temporary folder as model directory: C:\Users\TINHAN~1\AppData\Local\Temp\tmp40fu3k68
INFO:tensorflow:Using config: {'_service': None, '_save_checkpoints_secs': 600, '_session_config': None, '_task_id': 0, '_log_step_count_steps': 100, '_is_chief': True, '_keep_checkpoint_max': 5, '_master': '', '_num_worker_replicas': 1, '_keep_checkpoint_every_n_hours': 10000, '_save_checkpoints_steps': None, '_evaluation_master': '', '_tf_random_seed': None, '_cluster_spec': <tensorflow.python.training.server_lib.ClusterSpec object at 0x0000029778509710>, '_task_type': 'worker', '_device_fn': None, '_num_ps_replicas': 0, '_model_dir': 'C:\\Users\\TINHAN~1\\AppData\\Local\\Temp\\tmp40fu3k68', '_global_id_in_cluster': 0, '_save_summary_steps': 100, '_train_distribute': None}<jupyter_text>(5) Train and evaluate the model<jupyter_code># Train the estimator
train_input = tf.estimator.inputs.numpy_input_fn(
    x={"x": X_train_scaled},
    y=y_train,
    batch_size=50,
    shuffle=False,
    num_epochs=None)
estimator.train(input_fn = train_input,steps=1000) 
eval_input = tf.estimator.inputs.numpy_input_fn(
    x={"x": X_test_scaled},
    y=y_test, 
    shuffle=False,
    batch_size=X_test_scaled.shape[0],
    num_epochs=1)
estimator.evaluate(eval_input,steps=None)<jupyter_output>INFO:tensorflow:Calling model_fn.
INFO:tensorflow:Done calling model_fn.
INFO:tensorflow:Create CheckpointSaverHook.
INFO:tensorflow:Graph was finalized.
INFO:tensorflow:Running local_init_op.
INFO:tensorflow:Done running local_init_op.
INFO:tensorflow:Saving checkpoints for 0 into C:\Users\TINHAN~1\AppData\Local\Temp\tmp40fu3k68\model.ckpt.
INFO:tensorflow:loss = 3834.94, step = 1
INFO:tensorflow:global_step/sec: 680.489
INFO:tensorflow:loss = 6.5443, step = 101 (0.149 sec)
INFO:tensorflow:global_step/sec: 869.846
INFO:tensorflow:loss = 5.31314, step = 201 (0.115 sec)
INFO:tensorflow:global_step/sec: 806.707
INFO:tensorflow:loss = 6.1748, step = 301 (0.124 sec)
INFO:tensorflow:global_step/sec: 840.608
INFO:tensorflow:loss = 4.62847, step = 401 (0.119 sec)
INFO:tensorflow:global_step/sec: 800.255
INFO:tensorflow:loss = 5.12176, step = 501 (0.134 sec)
INFO:tensorflow:global_step/sec: 813.267
INFO:tensorflow:loss = 5.35003, step = 601 (0.114 sec)
INFO:tensorflow:global_step/sec: 775.443
[...]<jupyter_text>(6) Improve the model<jupyter_code>estimator_imp = tf.estimator.DNNRegressor(
    feature_columns=feature_columns,
    hidden_units=[300, 100],
    dropout=0.3,
    optimizer=tf.train.ProximalAdagradOptimizer(
      learning_rate=0.01,
      l1_regularization_strength=0.01, 
      l2_regularization_strength=0.01
    ))
estimator_imp.train(input_fn = train_input,steps=1000) 
estimator_imp.evaluate(eval_input,steps=None)<jupyter_output>INFO:tensorflow:Using default config.
WARNING:tensorflow:Using temporary folder as model directory: C:\Users\TINHAN~1\AppData\Local\Temp\tmpoan4nk2h
INFO:tensorflow:Using config: {'_service': None, '_save_checkpoints_secs': 600, '_session_config': None, '_task_id': 0, '_log_step_count_steps': 100, '_is_chief': True, '_keep_checkpoint_max': 5, '_master': '', '_num_worker_replicas': 1, '_keep_checkpoint_every_n_hours': 10000, '_save_checkpoints_steps': None, '_evaluation_master': '', '_tf_random_seed': None, '_cluster_spec': <tensorflow.python.training.server_lib.ClusterSpec object at 0x0000029778EC56A0>, '_task_type': 'worker', '_device_fn': None, '_num_ps_replicas': 0, '_model_dir': 'C:\\Users\\TINHAN~1\\AppData\\Local\\Temp\\tmpoan4nk2h', '_global_id_in_cluster': 0, '_save_summary_steps': 100, '_train_distribute': None}
INFO:tensorflow:Calling model_fn.
INFO:tensorflow:Done calling model_fn.
INFO:tensorflow:Create CheckpointSaverHook.
INFO:tensorflow:Graph was finalized.
INFO:tensorfl[...]
{'fraction_non_alphanumeric': 0.13425549227013833, 'fraction_numerical': 0.05191212367778682, 'mean_word_length': 7.3732970027247955, 'pattern_counts': {'":': 2, '<': 26, '<?xml version=': 0, '>': 29, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '7', 'dir': 'jupyter-structured-clean-dedup', 'id': '140229', 'license': 'MIT', 'n_tokens_mistral': 2464, 'n_tokens_neox': 2229, 'n_words': 415, 'path': 'Stock_Algorithms/Artificial_Neural_Network.ipynb', 'repo_name': 'sonukumarraj007/Deep-Learning-Machine-Learning-Stock', 'size': '6145'}