<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Shantanu Singh on Medium]]></title>
        <description><![CDATA[Stories by Shantanu Singh on Medium]]></description>
        <link>https://medium.com/@shantanusingh_71633?source=rss-2c8c11320e0b------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*hm1qoFpC5RIKmxpex3m6Xg.jpeg</url>
            <title>Stories by Shantanu Singh on Medium</title>
            <link>https://medium.com/@shantanusingh_71633?source=rss-2c8c11320e0b------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Tue, 17 Nov 2026 03:30:14 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@shantanusingh_71633/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Regression in Neural Networks using TensorFlow(Low Level APIs)]]></title>
            <link>https://medium.com/@shantanusingh_71633/regression-in-neural-networks-using-tensorflow-low-level-apis-818b8bb7b433?source=rss-2c8c11320e0b------2</link>
            <guid isPermaLink="false">https://medium.com/p/818b8bb7b433</guid>
            <category><![CDATA[regression]]></category>
            <category><![CDATA[neural-networks]]></category>
            <category><![CDATA[tensorflow]]></category>
            <category><![CDATA[machine-learning]]></category>
            <dc:creator><![CDATA[Shantanu Singh]]></dc:creator>
            <pubDate>Fri, 06 Jul 2018 07:09:37 GMT</pubDate>
            <atom:updated>2018-07-06T10:28:51.094Z</atom:updated>
            <content:encoded><![CDATA[<p>Mostly all courses and blogs I read on neural networks talk about how neural networks work and are used for classification it is actually easy to understand how same neural nets can used for regression tasks such as housing price prediction and stock price prediction but it actually took me some time to understand how to perform such tasks using neural nets so i decided to write a blog on it.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/278/1*aLSLLlIV4DnrY8uVYAY3FA.png" /><figcaption>Neural network with 2 hidden layer and single output</figcaption></figure><p>All neural networks that perform multiclass<strong> </strong>classification mostly have softmax layer as the last layer which outputs the probability of each class as output. In case of regression we don’t need any probability but actual value of predicted output so we just have to replace the softmax layer with a normal hidden layer and compute the loss using mean square error function .</p><p><strong>MSE = 1/n*∑(Y_predicted−Y)^2</strong></p><p>The complete implementation in tensorflow is available <a href="https://github.com/shan1322/Stock-blog">here</a> but i would suggest you to go through the explain first</p><p><strong>Lets look at the data preprocessing and Neural Network model for building a model that predicts stock prices</strong></p><pre>import pandas as pd<br>import numpy as np<br>import matplotlib.pyplot as plt<br>from sklearn.utils import shuffle<br>#<strong>Pre processing of data</strong><br>df = pd.read_csv(&#39;data/data1.csv&#39;)<br>df = df.drop(&#39;Date&#39;,1)<br><strong>#col_list.remove(&#39;Volume&#39;)</strong><br>col_list = df.columns.tolist()<br>df[&#39;Volume&#39;] = df[&#39;Volume&#39;] /10<br>df.to_csv(&quot;data/proce.csv&quot;, index=False)<br>validate_df = pd.read_csv(&#39;data/proce.csv&#39;)<br>validate_df.head()<br>df=pd.read_csv(&quot;data/proce.csv&quot;)<br>df = shuffle(df)# shuffle dataset<br>y=df[&#39;Open&#39;]#target<br>df=df.drop(&#39;OpenInt&#39;,axis=1)<br>df=df.drop(&#39;Open&#39;,axis=1)<br>df=np.array(df)<br>print(len(df),len(y))<br>#labels and features<strong> </strong><br>y_train,y_test=np.reshape(np.array(y[0:1000]),(1000,1)),np.reshape(np.array(y[1000:1232]),(232,1)<strong>)</strong>#train,test label <br>print(y_train.shape,y_test.shape)<br>x_train,x_test=np.array(df[0:1000]),np.array(df[1000:1232])#train and test features<br>print(x_train.shape,x_test.shape)</pre><p>The above code is for preparing data to feed into neural network the code for initial part is in data.ipynb and predict.ipynb ipython notebooks</p><pre>X = tf.placeholder(&quot;float&quot;, [None, 4])#4 features high,low,close and volume<br>Y = tf.placeholder(&quot;float&quot;, [None, 1])#1 output label open</pre><p>In above code we initialize placeholders for labels and features placeholders are used so that data can be fed at a later time(during training,testing,prediction) and value is set to None because any number of samples can be used as input .</p><pre>weights = {<br>    &#39;h1&#39;: tf.Variable(tf.random_normal([4, 10])),#4 inputs 10  nodes in h1 layer<br>    &#39;h2&#39;: tf.Variable(tf.random_normal([10, 10])),# 10 nodes in h2 layer<br>    &#39;out&#39;: tf.Variable(tf.random_normal([10, 1]))# 1 ouput label<br>}<br>biases = {<br>    &#39;b1&#39;: tf.Variable(tf.random_normal([10])),<br>    &#39;b2&#39;: tf.Variable(tf.random_normal([10])),<br>    &#39;out&#39;: tf.Variable(tf.random_normal([1]))<br>}</pre><p>We initialize weights biases for hidden layers</p><pre>def neural_net(x):<br>    #hidden layer 1<br>    layer_1 = tf.add(tf.matmul(x, weights[&#39;h1&#39;]), biases[&#39;b1&#39;])<br>    layer_1 = tf.nn.relu(layer_1)#activation<br>    #hideen layer 2<br>    layer_2 = tf.add(tf.matmul(layer_1, weights[&#39;h2&#39;]), biases[&#39;b2&#39;])<br>    layer_1 = tf.nn.relu(layer_2)#activation<br>    # output layer<br>    out_layer = tf.matmul(layer_2, weights[&#39;out&#39;]) + biases[&#39;out&#39;]<br>    return (out_layer)</pre><p>We build the neural network model with each layer output being relu(W(transpose)*X +b) where relu is rectified linear unit which is used as an activation function.</p><pre>Y_hat=neural_net(X)<br>loss_op=tf.losses.mean_squared_error(Y,Y_hat)#loss function<br>optimizer = tf.train.AdamOptimizer(learning_rate=0.01)#define optimizer # play around with learning rate<br>train_op = optimizer.minimize(loss_op)#minimize losss<br>init = tf.global_variables_initializer()<br>epoch=2000</pre><p>Y_hat is the predicted output value .We define loss function as MSE and use Adam optimizer and go through data 2000 times you can always play around with the learning rate, activation functions and use different types of optimizer’s.</p><pre>with tf.Session() as sess:<br>    sess.run(init)<br>    for i in range(0,epoch):<br>        sess.run(train_op,feed_dict={X:x_train,Y:y_train})<br>        loss=sess.run(loss_op,feed_dict={X:x_train,Y:y_train})<br>        if(i%100==0):<br>            print(&quot;epoch no&quot;+str(i),(loss))<br>        pred=sess.run(Y_hat,feed_dict={X:x_test}</pre><p>feed_dict is used to feed data to placeholders and pred is the actual output on the test set .</p><pre>plt.plot((pred), color=&#39;red&#39;, label=&#39;Prediction&#39;)<br>plt.plot(y_test, color=&#39;blue&#39;, label=&#39;Orignal&#39;)<br>plt.legend(loc=&#39;upper left&#39;)<br>plt.show()</pre><p>plot the predicted labels and ground truth values</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/495/1*ku9gZ3J4aUIGoVV1yjw84g.png" /><figcaption>Output and ground truth</figcaption></figure><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=818b8bb7b433" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>