In [325]:
import tensorflow as tf
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
%matplotlib inline
from sklearn.cross_validation import train_test_split

In [326]:
data = pd.read_csv("https://raw.githubusercontent.com/ml6973/Course/master/code/data/intro_to_ann.csv")
X, y = np.array(data.ix[:,0:2]), np.array(data.ix[:,2:3])
print(X.shape, y.shape)
X_train, X_test, y_train, y_test = train_test_split(X,y,test_size=1)
print(X_train.shape, X_test.shape, y_train.shape, y_test.shape)
plt.scatter(X[:,0], X[:,1], s=40, c=y, cmap=plt.cm.BuGn)


(500, 2) (500, 1)
(499, 2) (1, 2) (499, 1) (1, 1)
Out[326]:
<matplotlib.collections.PathCollection at 0x7f827d9fff28>
/usr/lib/python3/dist-packages/matplotlib/collections.py:549: FutureWarning: elementwise comparison failed; returning scalar instead, but in the future will perform elementwise comparison
  if self._edgecolors == 'face':

In [327]:
epochs = 1000
learning_rate = 0.01
hl_nodes = 5
num_features = 2
num_output = 1

In [328]:
x = tf.placeholder(tf.float32, [None, num_features])
y_ = tf.placeholder(tf.float32, [None, num_output])

# Create model
def multilayer_perceptron(x, weights, biases):
    # Hidden layer with sigmoid activation
    layer_1 = tf.add(tf.matmul(x, weights['h1']), biases['b1'])
    layer_1 = tf.nn.softmax(layer_1)
    # Output layer with sigmoid activation
    out_layer = tf.matmul(layer_1, weights['out']) + biases['out']
    out_layer = tf.nn.sigmoid(out_layer)
    return out_layer

In [329]:
# Store layers weight & bias
weights = {
    'h1': tf.Variable(tf.random_normal([num_features, hl_nodes])),
    'out': tf.Variable(tf.random_normal([hl_nodes, num_output]))
}
biases = {
    'b1': tf.Variable(tf.random_normal([hl_nodes])),
    'out': tf.Variable(tf.random_normal([num_output]))
}

# Construct model
pred = multilayer_perceptron(x, weights, biases)

In [330]:
# Define loss and optimizer
cross_entropy = tf.reduce_mean(-tf.reduce_sum(y_ * tf.log(pred), reduction_indices=[1]))
optimizer = tf.train.GradientDescentOptimizer(learning_rate).minimize(cross_entropy)

# Initializing the variables
init = tf.initialize_all_variables()

In [331]:
# Launch the graph
with tf.Session() as sess:
    sess.run(init)
    for i in range(epochs):
        sess.run([optimizer], feed_dict={x: X_train, y_: y_train})
        print(sess.run([cross_entropy], feed_dict={x: X_train, y_: y_train}))


[0.25093549]
[0.25038719]
[0.24984089]
[0.24929652]
[0.24875408]
[0.24821357]
[0.24767502]
[0.24713837]
[0.24660362]
[0.24607077]
[0.24553977]
[0.2450107]
[0.2444835]
[0.24395816]
[0.2434347]
[0.24291307]
[0.2423933]
[0.24187531]
[0.24135917]
[0.2408448]
[0.24033231]
[0.2398216]
[0.23931266]
[0.23880547]
[0.23830013]
[0.23779652]
[0.23729463]
[0.23679456]
[0.23629619]
[0.23579955]
[0.23530468]
[0.2348115]
[0.23432006]
[0.23383027]
[0.23334222]
[0.23285584]
[0.23237115]
[0.23188813]
[0.23140673]
[0.23092709]
[0.23044902]
[0.22997266]
[0.22949788]
[0.22902475]
[0.22855325]
[0.22808336]
[0.22761509]
[0.2271484]
[0.22668332]
[0.22621979]
[0.22575787]
[0.22529756]
[0.22483876]
[0.22438155]
[0.22392586]
[0.22347176]
[0.22301914]
[0.22256812]
[0.22211859]
[0.22167057]
[0.22122407]
[0.22077911]
[0.22033562]
[0.21989362]
[0.2194531]
[0.21901405]
[0.21857651]
[0.21814041]
[0.21770577]
[0.21727262]
[0.21684086]
[0.21641059]
[0.21598174]
[0.2155543]
[0.2151283]
[0.21470368]
[0.21428053]
[0.2138588]
[0.21343835]
[0.21301942]
[0.2126018]
[0.21218558]
[0.21177076]
[0.21135727]
[0.21094516]
[0.21053441]
[0.21012498]
[0.2097169]
[0.20931022]
[0.2089048]
[0.20850076]
[0.20809802]
[0.20769659]
[0.20729648]
[0.20689766]
[0.20650014]
[0.20610394]
[0.205709]
[0.20531535]
[0.20492297]
[0.20453186]
[0.20414202]
[0.20375347]
[0.20336616]
[0.20298006]
[0.20259526]
[0.20221168]
[0.20182931]
[0.20144819]
[0.20106828]
[0.2006896]
[0.20031215]
[0.19993591]
[0.19956085]
[0.19918697]
[0.19881433]
[0.19844285]
[0.19807257]
[0.19770344]
[0.19733551]
[0.19696872]
[0.19660312]
[0.19623868]
[0.19587538]
[0.19551323]
[0.19515222]
[0.19479236]
[0.19443364]
[0.19407605]
[0.19371957]
[0.19336422]
[0.19300999]
[0.19265683]
[0.19230482]
[0.1919539]
[0.19160408]
[0.19125536]
[0.19090769]
[0.19056115]
[0.19021568]
[0.1898713]
[0.18952793]
[0.18918565]
[0.18884447]
[0.18850428]
[0.1881652]
[0.18782715]
[0.18749014]
[0.18715419]
[0.18681926]
[0.18648535]
[0.18615244]
[0.18582062]
[0.18548979]
[0.18515998]
[0.18483114]
[0.18450335]
[0.18417658]
[0.18385078]
[0.18352595]
[0.18320216]
[0.18287931]
[0.18255743]
[0.18223654]
[0.18191665]
[0.18159772]
[0.18127979]
[0.18096277]
[0.18064672]
[0.18033168]
[0.18001752]
[0.17970432]
[0.17939207]
[0.17908074]
[0.17877038]
[0.17846094]
[0.1781524]
[0.17784481]
[0.17753816]
[0.17723235]
[0.17692757]
[0.17662361]
[0.17632057]
[0.17601846]
[0.17571722]
[0.1754169]
[0.17511743]
[0.17481887]
[0.17452121]
[0.17422442]
[0.17392851]
[0.17363346]
[0.17333929]
[0.17304599]
[0.17275357]
[0.17246199]
[0.17217124]
[0.17188136]
[0.17159234]
[0.17130415]
[0.17101683]
[0.17073035]
[0.1704447]
[0.17015983]
[0.16987586]
[0.16959269]
[0.16931036]
[0.1690288]
[0.16874813]
[0.16846821]
[0.16818914]
[0.16791084]
[0.1676334]
[0.1673567]
[0.16708082]
[0.16680573]
[0.16653147]
[0.16625798]
[0.16598524]
[0.16571333]
[0.16544215]
[0.16517176]
[0.16490221]
[0.16463336]
[0.16436528]
[0.16409799]
[0.16383146]
[0.16356568]
[0.16330065]
[0.16303638]
[0.16277285]
[0.16251005]
[0.16224805]
[0.16198675]
[0.16172619]
[0.16146636]
[0.16120727]
[0.16094895]
[0.16069129]
[0.1604344]
[0.16017821]
[0.15992278]
[0.159668]
[0.15941396]
[0.15916066]
[0.15890802]
[0.15865611]
[0.15840493]
[0.15815438]
[0.1579046]
[0.15765545]
[0.15740702]
[0.15715931]
[0.15691225]
[0.15666588]
[0.1564202]
[0.15617523]
[0.15593088]
[0.15568724]
[0.15544425]
[0.15520194]
[0.15496029]
[0.15471931]
[0.15447897]
[0.15423934]
[0.15400031]
[0.15376195]
[0.15352423]
[0.1532872]
[0.15305081]
[0.15281501]
[0.15257992]
[0.15234543]
[0.1521116]
[0.1518784]
[0.15164579]
[0.15141389]
[0.15118256]
[0.15095186]
[0.1507218]
[0.15049233]
[0.15026352]
[0.15003532]
[0.14980771]
[0.1495807]
[0.14935432]
[0.14912856]
[0.14890341]
[0.14867885]
[0.14845487]
[0.14823151]
[0.14800873]
[0.14778657]
[0.14756498]
[0.14734399]
[0.14712358]
[0.14690377]
[0.14668448]
[0.14646585]
[0.14624776]
[0.14603026]
[0.14581335]
[0.14559701]
[0.14538123]
[0.14516601]
[0.14495136]
[0.14473729]
[0.14452377]
[0.14431077]
[0.14409837]
[0.14388654]
[0.14367524]
[0.14346451]
[0.14325434]
[0.1430447]
[0.14283559]
[0.14262703]
[0.14241906]
[0.14221157]
[0.14200465]
[0.14179826]
[0.14159238]
[0.14138709]
[0.14118229]
[0.14097802]
[0.14077426]
[0.14057107]
[0.14036837]
[0.14016621]
[0.1399646]
[0.13976344]
[0.13956283]
[0.13936275]
[0.13916317]
[0.13896409]
[0.13876553]
[0.13856746]
[0.13836992]
[0.13817288]
[0.13797635]
[0.13778029]
[0.13758475]
[0.13738967]
[0.13719514]
[0.13700107]
[0.1368075]
[0.13661441]
[0.13642183]
[0.13622971]
[0.13603809]
[0.13584697]
[0.13565628]
[0.13546611]
[0.13527639]
[0.13508716]
[0.13489842]
[0.13471015]
[0.13452235]
[0.13433501]
[0.13414814]
[0.13396174]
[0.13377579]
[0.13359031]
[0.1334053]
[0.13322076]
[0.13303666]
[0.132853]
[0.13266984]
[0.13248709]
[0.13230483]
[0.13212299]
[0.13194163]
[0.1317607]
[0.13158022]
[0.13140017]
[0.13122056]
[0.13104139]
[0.1308627]
[0.13068441]
[0.13050658]
[0.13032918]
[0.1301522]
[0.12997565]
[0.12979954]
[0.12962388]
[0.12944859]
[0.12927379]
[0.12909938]
[0.12892543]
[0.12875186]
[0.12857872]
[0.12840602]
[0.12823373]
[0.12806185]
[0.12789041]
[0.12771933]
[0.12754871]
[0.12737846]
[0.12720865]
[0.12703924]
[0.12687026]
[0.12670167]
[0.12653345]
[0.12636568]
[0.12619828]
[0.12603129]
[0.12586471]
[0.12569851]
[0.12553275]
[0.12536733]
[0.12520234]
[0.12503773]
[0.1248735]
[0.12470968]
[0.12454624]
[0.12438318]
[0.12422053]
[0.12405823]
[0.12389633]
[0.12373481]
[0.12357368]
[0.12341291]
[0.12325253]
[0.12309253]
[0.1229329]
[0.12277365]
[0.12261478]
[0.12245626]
[0.12229814]
[0.12214037]
[0.12198297]
[0.12182594]
[0.12166928]
[0.12151299]
[0.12135705]
[0.12120148]
[0.12104627]
[0.12089144]
[0.12073692]
[0.1205828]
[0.12042902]
[0.12027561]
[0.12012253]
[0.11996984]
[0.1198175]
[0.11966547]
[0.11951382]
[0.1193625]
[0.11921153]
[0.11906092]
[0.11891066]
[0.11876073]
[0.11861115]
[0.11846191]
[0.11831304]
[0.11816446]
[0.11801624]
[0.11786836]
[0.11772081]
[0.1175736]
[0.11742672]
[0.11728018]
[0.11713398]
[0.1169881]
[0.11684257]
[0.11669735]
[0.11655247]
[0.11640789]
[0.11626365]
[0.11611977]
[0.11597618]
[0.11583292]
[0.11568999]
[0.11554737]
[0.11540506]
[0.1152631]
[0.11512142]
[0.11498007]
[0.11483905]
[0.11469832]
[0.11455792]
[0.11441784]
[0.11427808]
[0.11413862]
[0.11399946]
[0.11386061]
[0.11372207]
[0.11358384]
[0.11344592]
[0.11330831]
[0.11317099]
[0.113034]
[0.1128973]
[0.11276088]
[0.11262479]
[0.11248898]
[0.11235349]
[0.11221829]
[0.11208336]
[0.11194877]
[0.11181443]
[0.11168042]
[0.11154667]
[0.11141326]
[0.11128011]
[0.11114725]
[0.11101469]
[0.11088242]
[0.11075042]
[0.11061873]
[0.1104873]
[0.11035619]
[0.11022535]
[0.11009478]
[0.10996452]
[0.10983451]
[0.1097048]
[0.10957537]
[0.10944621]
[0.10931735]
[0.10918874]
[0.10906041]
[0.10893238]
[0.10880461]
[0.10867711]
[0.10854989]
[0.10842294]
[0.10829626]
[0.10816983]
[0.10804371]
[0.10791785]
[0.10779223]
[0.1076669]
[0.10754186]
[0.10741705]
[0.10729251]
[0.10716825]
[0.10704425]
[0.1069205]
[0.10679702]
[0.10667381]
[0.10655086]
[0.10642816]
[0.10630573]
[0.10618354]
[0.10606162]
[0.10593995]
[0.10581855]
[0.10569742]
[0.10557652]
[0.10545588]
[0.10533549]
[0.10521534]
[0.10509546]
[0.10497583]
[0.10485642]
[0.1047373]
[0.10461842]
[0.10449978]
[0.10438138]
[0.10426323]
[0.10414535]
[0.1040277]
[0.10391027]
[0.1037931]
[0.10367618]
[0.1035595]
[0.10344306]
[0.10332687]
[0.1032109]
[0.10309517]
[0.1029797]
[0.10286445]
[0.10274947]
[0.10263468]
[0.10252015]
[0.10240585]
[0.10229178]
[0.10217795]
[0.10206436]
[0.10195097]
[0.10183786]
[0.10172495]
[0.10161228]
[0.10149983]
[0.10138763]
[0.10127563]
[0.10116387]
[0.10105235]
[0.10094105]
[0.10082997]
[0.10071912]
[0.10060849]
[0.10049808]
[0.1003879]
[0.10027794]
[0.10016821]
[0.1000587]
[0.099949405]
[0.099840336]
[0.099731483]
[0.099622853]
[0.099514417]
[0.099406227]
[0.099298239]
[0.099190466]
[0.099082947]
[0.098975591]
[0.098868467]
[0.098761573]
[0.098654881]
[0.098548405]
[0.098442137]
[0.098336071]
[0.098230213]
[0.098124586]
[0.098019175]
[0.097913951]
[0.097808935]
[0.097704127]
[0.097599551]
[0.097495161]
[0.09739098]
[0.097287007]
[0.097183235]
[0.097079679]
[0.096976325]
[0.096873157]
[0.09677022]
[0.096667454]
[0.096564911]
[0.096462555]
[0.0963604]
[0.096258461]
[0.096156701]
[0.096055165]
[0.095953815]
[0.095852666]
[0.095751718]
[0.095650956]
[0.095550381]
[0.095450014]
[0.095349848]
[0.095249869]
[0.095150076]
[0.095050484]
[0.094951078]
[0.094851889]
[0.094752871]
[0.094654061]
[0.094555423]
[0.094456971]
[0.094358727]
[0.094260655]
[0.094162807]
[0.094065115]
[0.093967617]
[0.093870305]
[0.093773179]
[0.093676254]
[0.093579493]
[0.093482919]
[0.093386561]
[0.093290359]
[0.093194336]
[0.093098506]
[0.093002856]
[0.092907406]
[0.092812121]
[0.092717014]
[0.092622094]
[0.092527352]
[0.092432797]
[0.09233842]
[0.0922442]
[0.092150196]
[0.092056349]
[0.091962673]
[0.091869175]
[0.091775864]
[0.091682725]
[0.091589764]
[0.091496967]
[0.091404349]
[0.091311924]
[0.091219656]
[0.09112756]
[0.091035619]
[0.09094388]
[0.090852298]
[0.090760909]
[0.090669669]
[0.090578601]
[0.090487696]
[0.090396978]
[0.090306409]
[0.090216033]
[0.090125814]
[0.090035744]
[0.089945875]
[0.089856163]
[0.089766592]
[0.089677207]
[0.089587986]
[0.089498945]
[0.089410037]
[0.089321308]
[0.08923275]
[0.089144349]
[0.089056119]
[0.088968046]
[0.088880122]
[0.088792384]
[0.088704795]
[0.08861737]
[0.088530093]
[0.088442989]
[0.088356033]
[0.088269241]
[0.088182613]
[0.088096142]
[0.088009804]
[0.087923661]
[0.087837681]
[0.087751843]
[0.087666146]
[0.087580629]
[0.087495252]
[0.087410025]
[0.087324962]
[0.087240063]
[0.087155297]
[0.087070704]
[0.086986244]
[0.086901948]
[0.086817794]
[0.086733803]
[0.086649962]
[0.086566254]
[0.086482711]
[0.086399317]
[0.086316071]
[0.086232997]
[0.086150043]
[0.086067259]
[0.085984617]
[0.085902102]
[0.085819766]
[0.085737564]
[0.085655503]
[0.085573584]
[0.085491821]
[0.085410208]
[0.085328728]
[0.085247405]
[0.085166231]
[0.085085168]
[0.085004292]
[0.084923543]
[0.084842928]
[0.084762454]
[0.084682129]
[0.084601961]
[0.084521912]
[0.084442019]
[0.084362268]
[0.084282622]
[0.084203161]
[0.08412382]
[0.084044628]
[0.083965577]
[0.083886653]
[0.083807878]
[0.083729237]
[0.083650723]
[0.083572373]
[0.083494119]
[0.083416052]
[0.083338097]
[0.083260268]
[0.083182581]
[0.083105028]
[0.083027609]
[0.082950324]
[0.082873188]
[0.082796186]
[0.082719304]
[0.082642555]
[0.082565948]
[0.082489483]
[0.082413137]
[0.082336925]
[0.08226084]
[0.082184896]
[0.082109079]
[0.082033388]
[0.081957832]
[0.081882402]
[0.081807107]
[0.081731938]
[0.081656903]
[0.081581995]
[0.081507213]
[0.081432559]
[0.081358038]
[0.081283629]
[0.081209369]
[0.081135213]
[0.081061199]
[0.080987327]
[0.080913574]
[0.080839932]
[0.08076641]
[0.080693044]
[0.080619767]
[0.080546632]
[0.080473624]
[0.080400728]
[0.08032798]
[0.080255322]
[0.080182806]
[0.080110416]
[0.08003813]
[0.079965986]
[0.079893969]
[0.079822063]
[0.079750277]
[0.079678617]
[0.079607084]
[0.079535641]
[0.079464324]
[0.079393156]
[0.079322077]
[0.079251148]
[0.079180323]
[0.079109617]
[0.07903903]
[0.078968577]
[0.078898206]
[0.078827985]
[0.078757882]
[0.078687869]
[0.078617975]
[0.078548215]
[0.078478567]
[0.078409038]
[0.078339621]
[0.078270316]
[0.07820113]
[0.078132063]
[0.078063101]
[0.077994257]
[0.077925518]
[0.077856906]
[0.077788398]
[0.077720009]
[0.077651709]
[0.077583559]
[0.077515505]
[0.077447556]
[0.077379733]
[0.077312008]
[0.077244408]
[0.077176906]
[0.077109516]
[0.077042229]
[0.076975077]
[0.076908022]
[0.076841086]
[0.07677424]
[0.076707505]
[0.076640896]
[0.076574385]
[0.076507986]
[0.076441698]
[0.076375499]
[0.076309428]
[0.07624346]
[0.076177604]
[0.076111853]
[0.076046206]
[0.075980648]
[0.07591521]
[0.075849891]
[0.075784653]
[0.075719558]
[0.075654544]
[0.075589627]
[0.075524829]
[0.075460121]
[0.075395525]
[0.07533104]
[0.075266652]
[0.075202361]
[0.075138181]
[0.075074106]
[0.075010136]
[0.074946262]
[0.074882492]
[0.07481882]
[0.074755274]
[0.07469181]
[0.074628443]
[0.074565172]
[0.074502029]
[0.074438952]
[0.074376002]
[0.074313134]
[0.07425037]
[0.074187711]
[0.074125163]
[0.074062712]
[0.074000344]
[0.073938072]
[0.073875919]
[0.073813848]
[0.073751889]
[0.073690012]
[0.073628254]
[0.073566556]
[0.073504992]
[0.073443502]
[0.073382132]
[0.073320828]
[0.073259652]
[0.073198557]
[0.073137559]
[0.073076658]
[0.073015846]
[0.072955139]
[0.072894514]
[0.072834]
[0.072773576]
[0.072713234]
[0.072653003]
[0.072592847]
[0.07253281]
[0.072472848]
[0.07241299]
[0.072353229]
[0.072293535]
[0.07223396]
[0.072174467]
[0.072115071]
[0.072055764]
[0.071996555]
[0.071937434]
[0.071878396]
[0.071819469]
[0.07176061]
[0.071701854]
[0.071643181]
[0.071584612]
[0.071526125]
[0.071467735]
[0.071409442]
[0.071351215]
[0.071293093]
[0.071235046]
[0.071177103]
[0.071119241]
[0.071061477]
[0.07100378]
[0.070946202]
[0.070888683]
[0.070831269]
[0.070773944]
[0.070716694]
[0.070659555]
[0.070602477]
[0.070545502]
[0.070488617]
[0.070431814]
[0.070375092]
[0.070318453]
[0.070261918]
[0.070205435]
[0.070149072]
[0.070092782]
[0.070036583]
[0.069980465]
[0.069924437]
[0.069868475]
[0.069812603]
[0.069756828]
[0.069701135]
[0.069645509]
[0.06958998]
[0.06953454]
[0.069479167]
[0.069423884]
[0.069368698]
[0.069313571]
[0.069258541]
[0.069203593]
[0.069148727]
[0.069093943]
[0.06903924]
[0.068984613]
[0.068930082]

In [317]:


In [ ]: