예제 #1
0
""" import your model here """
import your_model as tf
""" your model should support the following code """

# create model
x = tf.placeholder(tf.float32, [None, 784])
W = tf.Variable(tf.zeros([784, 10]))
b = tf.Variable(tf.zeros([10]))
y = tf.nn.softmax(tf.matmul(x, W) + b)

# define loss and optimizer
y_ = tf.placeholder(tf.float32, [None, 10])

cross_entropy = tf.reduce_mean(
    -tf.reduce_sum(y_ * tf.log(y), reduction_indices=[1]))

W_grad = tf.gradients(cross_entropy, [W])[0]
train_step = tf.assign(W, W - 0.5 * W_grad)

sess = tf.Session()
sess.run(tf.global_variables_initializer())

# get the mnist dataset (use tensorflow here)
from tensorflow.examples.tutorials.mnist import input_data
mnist = input_data.read_data_sets("MNIST_data/", one_hot=True)

# train
for _ in range(1000):
    batch_xs, batch_ys = mnist.train.next_batch(100)
    sess.run(train_step, feed_dict={x: batch_xs, y_: batch_ys})
예제 #2
0
def weight_variable(shape):
    initial = tf.random_normal(shape, stddev=0.1)
    return tf.Variable(initial)
예제 #3
0
""" import your model here """
import your_model as tf
""" your model should support the following code """

import numpy as np

sess = tf.Session()

# linear model
W = tf.Variable([.5], dtype=tf.float32)
b = tf.Variable([1.5], dtype=tf.float32)
x = tf.placeholder(tf.float32)

linear_model = W * x + b

# define error
y = tf.placeholder(tf.float32)
error = tf.reduce_sum(linear_model - y)

# run init
init = tf.global_variables_initializer()
sess.run(init)

# calc error
feed = {x: [1, 2, 3, 4], y: [0, -1, -2, -3]}

# assign
fixW = tf.assign(W, [-1.0])
fixb = tf.assign(b, [1.])
sess.run([fixW, fixb])
ans = sess.run(error, feed)
예제 #4
0
def bias_variable(shape):
    initial = tf.constant(0.1, shape=shape)
    return tf.Variable(initial)
예제 #5
0
# Create model
def multilayer_perceptron(x, weights, biases):
    # Hidden layer with RELU activation
    layer_1 = tf.matmul(x, weights['h1']) + biases['b1']
    layer_1 = tf.nn.relu(layer_1)
    # Hidden layer with RELU activation
    layer_2 = tf.matmul(layer_1, weights['h2']) + biases['b2']
    layer_2 = tf.nn.relu(layer_2)
    # Output layer with linear activation
    out_layer = tf.matmul(layer_2, weights['out']) + biases['out']
    return out_layer


# Store layers weight & bias
weights = {
    'h1': tf.Variable(tf.zeros([n_input, n_hidden_1])),
    'h2': tf.Variable(tf.zeros([n_hidden_1, n_hidden_2])),
    'out': tf.Variable(tf.random_normal([n_hidden_2, n_classes]))
}
biases = {
    'b1': tf.Variable(tf.random_normal([n_hidden_1])),
    'b2': tf.Variable(tf.random_normal([n_hidden_2])),
    'out': tf.Variable(tf.random_normal([n_classes]))
}

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

# Define loss and optimizer
cost = tf.reduce_mean(
    tf.nn.softmax_cross_entropy_with_logits(logits=pred, labels=y))