Skip to content

Instantly share code, notes, and snippets.

def neural_net_model(inputs, mode):
with tf.variable_scope('ConvModel'):
inputs = inputs / 255
input_layer = tf.reshape(inputs, [-1, 28, 28, 1])
conv1 = tf.layers.conv2d(
inputs=input_layer,
filters=20,
kernel_size=[5, 5],
padding='valid',
activation=tf.nn.relu)
def model_fn(features, labels, mode):
logits = neural_net_model(features, mode)
class_prediction = tf.argmax(logits, axis=-1)
preds = class_prediction
loss = None
train_op = None
eval_metric_ops = {}
if mode in (tf.estimator.ModeKeys.EVAL, tf.estimator.ModeKeys.TRAIN):
def model_fn(features, labels, mode, params, config)
iris = datasets.load_iris()
X = iris.data[:, :2]
Y = iris.target
clf = MLPClassifier(solver='lbfgs', hidden_layer_sizes=(10,10))
# Create an instance of Logistic Regression Classifier and fit the data.
clf.fit(X, Y)
# Prediction phase
# Build 2 hidden layer DNN with 10, 10 units respectively.
classifier = tf.estimator.DNNClassifier(
feature_columns=my_feature_columns,
# Two hidden layers of 10 nodes each.
hidden_units=[10, 10],
# The model must choose between 3 classes.
n_classes=3,
# The directory which model to be saved
model_dir='./tmp'
)
_NumericColumn(key='SepalLength', shape=(1,), default_value=None, dtype=tf.float32, normalizer_fn=None)
_NumericColumn(key='SepalWidth', shape=(1,), default_value=None, dtype=tf.float32, normalizer_fn=None)
_NumericColumn(key='PetalLength', shape=(1,), default_value=None, dtype=tf.float32, normalizer_fn=None)
_NumericColumn(key='PetalWidth', shape=(1,), default_value=None, dtype=tf.float32, normalizer_fn=None)
def input_fn():
... # manipulate dataset, extracting the feature dict and the label
return feature_dict, label
# Feature columns describe how to use the input.
my_feature_columns = []
for key in iris_data.train_x.keys():
my_feature_columns.append(tf.feature_column.numeric_column(key=key))
# Build 2 hidden layer DNN with 10, 10 units respectively.
classifier = tf.estimator.DNNClassifier(
feature_columns=my_feature_columns,
# Two hidden layers of 10 nodes each.
hidden_units=[10, 10],
elems = np.array(['H', 'e', 'l', 'l', 'o', ' ', 'T', 'e', 'n', 's', 'o', 'r'])
sum = tf.scan(lambda a, x: a + x, elems)
>>> ['H' 'He' 'Hel' 'Hell' 'Hello' 'Hello ' 'Hello T' 'Hello Te'
'Hello Ten' 'Hello Tens' 'Hello Tenso' 'Hello Tensor']
training_data = np.random.rand(3,20)
training_labels = np.random.rand(3,1)
with tf.Session():
input_data = tf.constant(training_data)
input_labels = tf.constant(training_labels)
...