-1

I am trying to get a neural network going in TensforFlow. The dataset is simply length and width of a flower petal and the output can be either 1/0 depending on type:

x = [[3,1.5],
     [2,1],
     [4,1.5],
     [3,1],
     [3.5,0.5],
     [2,0.5],
     [5.5,1],
     [1,1]]

y = [1,
     0,
     1,
     0,
     1,
     0,
     1,
     0]

So far my code looks something like this:

define variables

x_1 = tf.placeholder(tf.float32, shape=[8,2])
y_1 = tf.placeholder(tf.float32, shape=[8])

w_1 = tf.placeholder(tf.float32, shape=[2,8])
b_1 = tf.placeholder(tf.float32, shape=[8,])

sess = tf.Session()

sess.run(tf.global_variables_initializer())

y_ = tf.matmul(x_1,w_1) + b

sigmoid = tf.nn.sigmoid(y_)

train_step = tf.train.GradientDescentOptimizer(0.5).minimize(sigmoid)

for _ in range(50000):

My question is how would I arrange my 'for' loop so that it takes the whole dataset at once and compares it to the actual output? The mnist dataset on tensorflow uses softmax cross entropy whereby you can specify the actual output and predicted output in the parameters of the function. However in this simple dataset how would I replicate the same in the remaining for loop so that the code grabs all data makes a prediction and compares it to the actual output? Also please specify if there is any problems with the shape of my variables thank you.

  • 1
    So far you are trying to minimize the output of your neural network. If you want your outputs to match y (aka the ground truth) maybe you should try minimizing a cost representing a difference between your output and y instead as a first step. – jeandut Apr 30 '18 at 14:22
  • 1
    Plus you should not use placeholders but Variables if you want to miniimze something. – jeandut Apr 30 '18 at 14:24
  • 1
    Long story short you better re-read carefully step-by-step tutorials. – jeandut Apr 30 '18 at 14:25
  • are there any tutorials you would recommend that explain this without using the mnist dataset – Himansu Odedra Apr 30 '18 at 14:37
  • [This](http://stackabuse.com/tensorflow-neural-network-tutorial/) looks great for getting started with the low level API, it uses the iris dataset (which is the same you seem to be using). – Alexander Harnisch Apr 30 '18 at 16:27

2 Answers2

0

You know, you can just use tflearn. Save a lot of time and frustration =)

import tflearn
from tflearn.layers.core import fully_connected,input_data
from tflearn.layers.estimator import regression

model = input_data(shape=[None,4,1])
model = fully_connected(model,1,activation='sigmoid')
model = regression(model)
model = tflearn.DNN(model)
model.fit(X_inputs=trainX,Y_targets=trainY,n_epoch=20,
          validation_set=(testX,testY),show_metric=True)
5Volts
  • 179
  • 3
  • 13
0

Manage to solve what i was looking for:

import tensorflow as tf
import numpy as np

train_X = np.asarray([[3,1.5],[2,1],[4,1.5],[3,1],[3.5,0.5],[2,0.5],[5.5,1],[1,1]])
train_Y = np.asarray([[1],[0],[1],[0],[1],[0],[1],[0]])

x = tf.placeholder("float",[None, 2])
y = tf.placeholder("float",[None, 1])

W = tf.Variable(tf.zeros([2, 1]))
b = tf.Variable(tf.zeros([1, 1]))

activation = tf.nn.sigmoid(tf.matmul(x, W)+b)
cost = tf.reduce_mean(tf.square(activation - y))
optimizer = tf.train.GradientDescentOptimizer(.2).minimize(cost)

init = tf.global_variables_initializer()

with tf.Session() as sess:
    sess.run(init)
    for i in range(50000):
        train_data = sess.run(optimizer, feed_dict={x: train_X, y: train_Y})

    result = sess.run(activation, feed_dict={x:train_X})
    print(result)