In [1]:
import warnings
warnings.filterwarnings('ignore')
In [2]:
%matplotlib inline
%pylab inline
In [3]:
# to make sure we have 0.18.1 as version of sklearn
!conda install --name root scikit-learn -y
In [4]:
# should be at least 0.18.1
import sklearn
sklearn.__version__
Out[4]:
In [5]:
# https://www.tensorflow.org/get_started/os_setup#anaconda_installation
!conda install --name root -c conda-forge tensorflow -y
In [6]:
import tensorflow as tf
tf.logging.set_verbosity(tf.logging.ERROR)
# should be at least 0.11.0
tf.__version__
Out[6]:
In [7]:
# graph definition
matrix1 = tf.constant([[3., 3.]])
matrix2 = tf.constant([[2.],[2.]])
product = tf.matmul(matrix1, matrix2)
# launching the graph in a session
with tf.Session() as sess:
result = sess.run([product])
print(result)
https://www.tensorflow.org/get_started/basic_usage#interactive_usage
In [8]:
sess = tf.InteractiveSession()
x = tf.Variable([1.0, 2.0])
a = tf.constant([3.0, 3.0])
# Initialize 'x' using the run() method of its initializer op.
x.initializer.run()
# Add an op to subtract 'a' from 'x'. Run it and print the result
sub = tf.sub(x, a)
print(sub.eval())
# ==> [-2. -1.]
# Close the Session when we're done.
sess.close()
In [9]:
from sklearn import datasets
iris = datasets.load_iris()
X = iris.data
y = iris.target
X.shape, y.shape
Out[9]:
In [10]:
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=3)
X_train.shape, y_train.shape, X_test.shape, y_test.shape
Out[10]:
In [18]:
feature_columns = [tf.contrib.layers.real_valued_column("", dimension=4)]
clf = tf.contrib.learn.DNNClassifier(feature_columns=feature_columns,
hidden_units=[10, 20, 10],
n_classes=3,
model_dir="/tmp/iris_model")
In [12]:
clf.fit(x=X_train, y=y_train, steps=2000)
Out[12]:
In [13]:
clf.evaluate(x=X_train, y=y_train)
Out[13]:
In [14]:
clf.evaluate(x=X_test, y=y_test)
Out[14]:
In [16]:
clf.predict(np.array([[6.4, 3.2, 4.5, 1.5], [5.8, 3.1, 5.0, 1.7]], dtype=float))
Out[16]:
In [ ]: