In [9]:
%matplotlib inline
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
import seaborn as sns
from theano.printing import pydotprint
import pickle

Linear Regression - Housing Data Set


In [3]:
import tensorflow as tf

In [11]:
with open('data/housing.dframe', 'rb') as dframe:
    housing = pickle.load(dframe)
x_train = np.stack([np.ones((len(housing['TAX']))), housing['RM']],axis=1)
y_train = np.array(housing['MEDV'])
# plt.scatter(x_train[:,1], y_train ,marker='x')
# housing.head()

In [13]:
x = tf.placeholder(tf.float32, [None, 2])
y = tf.placeholder(tf.float32, [None, 1])
theta = tf.Variable(tf.zeros([2, 1]))
h = tf.matmul(x, theta)
J = tf.reduce_sum(tf.pow(h - y,2)) / 2 * tf.size(x)[0] + tf.reduce_sum(tf.pow(theta,2))


---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-13-bb9905116611> in <module>()
      3 theta = tf.Variable(tf.zeros([2, 1]))
      4 h = tf.matmul(x, theta)
----> 5 J = tf.reduce_sum(tf.pow(h - y,2)) / 2 * tf.size(x)[0] + tf.reduce_sum(tf.pow(theta,2))

/home/jakob/miniconda2/envs/ml-software/lib/python3.5/site-packages/tensorflow/python/ops/array_ops.py in _SliceHelper(tensor, slice_spec)
    199       sizes.append(1)
    200       squeeze_dims.append(dim)
--> 201   sliced = slice(tensor, indices, sizes)
    202   if squeeze_dims:
    203     return squeeze(sliced, squeeze_dims=squeeze_dims)

/home/jakob/miniconda2/envs/ml-software/lib/python3.5/site-packages/tensorflow/python/ops/array_ops.py in slice(input_, begin, size, name)
    249     A `Tensor` the same type as `input`.
    250   """
--> 251   return gen_array_ops._slice(input_, begin, size, name=name)
    252 
    253 

/home/jakob/miniconda2/envs/ml-software/lib/python3.5/site-packages/tensorflow/python/ops/gen_array_ops.py in _slice(input, begin, size, name)
   1632   """
   1633   result = _op_def_lib.apply_op("Slice", input=input, begin=begin, size=size,
-> 1634                                 name=name)
   1635   return result
   1636 

/home/jakob/miniconda2/envs/ml-software/lib/python3.5/site-packages/tensorflow/python/ops/op_def_library.py in apply_op(self, op_type_name, name, **keywords)
    702           op = g.create_op(op_type_name, inputs, output_types, name=scope,
    703                            input_types=input_types, attrs=attr_protos,
--> 704                            op_def=op_def)
    705           outputs = op.outputs
    706           return _Restructure(ops.convert_n_to_tensor(outputs),

/home/jakob/miniconda2/envs/ml-software/lib/python3.5/site-packages/tensorflow/python/framework/ops.py in create_op(self, op_type, inputs, dtypes, input_types, name, attrs, op_def, compute_shapes, compute_device)
   2260                     original_op=self._default_original_op, op_def=op_def)
   2261     if compute_shapes:
-> 2262       set_shapes_for_outputs(ret)
   2263     self._add_op(ret)
   2264     self._record_op_seen_by_control_dependencies(ret)

/home/jakob/miniconda2/envs/ml-software/lib/python3.5/site-packages/tensorflow/python/framework/ops.py in set_shapes_for_outputs(op)
   1700       raise RuntimeError("No shape function registered for standard op: %s"
   1701                          % op.type)
-> 1702   shapes = shape_func(op)
   1703   if shapes is None:
   1704     raise RuntimeError(

/home/jakob/miniconda2/envs/ml-software/lib/python3.5/site-packages/tensorflow/python/ops/array_ops.py in _SliceShape(op)
   1052   ndims = begin_shape.merge_with(sizes_shape)[0].value
   1053   if ndims is not None:
-> 1054     input_shape.assert_has_rank(ndims)
   1055   begin_value = tensor_util.constant_value(op.inputs[1])
   1056   sizes_value = tensor_util.constant_value(op.inputs[2])

/home/jakob/miniconda2/envs/ml-software/lib/python3.5/site-packages/tensorflow/python/framework/tensor_shape.py in assert_has_rank(self, rank)
    619     """
    620     if self.ndims not in (None, rank):
--> 621       raise ValueError("Shape %s must have rank %d" % (self, rank))
    622 
    623   def with_rank(self, rank):

ValueError: Shape () must have rank 1

In [ ]:
theta = [0, 0]
rate = 0.01
n_epoch = 100
init = tf.initialize_all_variables()
sess = tf.Session()
sess.run(init)
for i in range(1000):
  batch_xs, batch_ys = mnist.train.next_batch(n_epoch)
  sess.run(train_step, feed_dict={x: batch_xs, y_: batch_ys})

In [12]:
line = np.linspace(x_train[:,1].min(),x_train[:,1].max())
y = f( np.stack([np.ones(len(line)),line],axis=1),theta)
plt.subplot(121)
plt.plot(hist)
plt.subplot(122)
plt.plot(line,y)
plt.scatter(x_train[:,1], y_train,marker='x')


---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-12-10982fa14d43> in <module>()
      1 line = np.linspace(x_train[:,1].min(),x_train[:,1].max())
----> 2 y = f( np.stack([np.ones(len(line)),line],axis=1),theta)
      3 plt.subplot(121)
      4 plt.plot(hist)
      5 plt.subplot(122)

NameError: name 'f' is not defined

Image Classification - MNIST dataset


In [ ]:

Image Classification - CIFAR-10 dataset


In [ ]: