Esempio n. 1
0
def linear_regression():
    epoch_number = 10
    learning_rate = 0.01
    train_features = [1.0, 2.0, 3.0, 4.0, 5.0]
    train_labels = [10.0, 20.0, 30.0, 40.0, 50.0]

    weights = tf.Variable(0.0)
    bias = tf.Variable(0.0)
    x = tf.placeholder(tf.float32)
    y = tf.placeholder(tf.float32)

    predict = weights * x + bias
    loss = y - predict
    sgd_optimizer = tf.train.GradientDescentOptimizer(learning_rate)
    train_op = sgd_optimizer.minimize(loss)

    with tf.Session() as sess:

        for epoch_index in range(epoch_number):
            # Take one sample from train dataset
            sample_number = len(train_features)
            train_feature = train_features[epoch_index % sample_number]
            train_label = train_labels[epoch_index % sample_number]

            # Update model variables and print loss
            sess.run(train_op, feed_dict={x: train_feature, y: train_label})
            loss_value = sess.run(loss, feed_dict={x: 1.0, y: 10.0})
            print("Epoch: {}, loss: {}, weight: {}, bias: {}".format(
                epoch_index, loss_value, sess.run(weights), sess.run(bias)))
Esempio n. 2
0
def main():
    a = tf.placeholder(tf.float32)
    b = tf.constant(32.0)
    c = tf.add(a, b)

    sess = tf.Session()
    print(sess.run(c, feed_dict={a: 10}))
    print(sess.run(c, feed_dict={a.name: 10}))
Esempio n. 3
0
def main():
    sess = tf.Session()

    hello = tf.constant("Hello, MiniFlow!")
    print(sess.run(hello))
    # "Hello, MiniFlow!"

    a = tf.constant(10)
    b = tf.constant(32)
    c = tf.add(a, b)
    print(sess.run(c))
    # 42

    sess = tf.Session()
    a = tf.placeholder(tf.float32)
    b = tf.constant(32.0)
    c = tf.add(a, b)
    print(sess.run(c, feed_dict={a: 10.0}))
    print(sess.run(c, feed_dict={a.name: 10.0}))
Esempio n. 4
0
train_X = mf.DataUtils(filename=trainfile_X).getImage()
train_y = mf.DataUtils(filename=trainfile_y).getLabel()
test_X = mf.DataUtils(testfile_X).getImage()
test_y = mf.DataUtils(testfile_y).getLabel()
train_X = np.array(train_X)
train_y = np.array(train_y)
test_X = np.array(test_X)
test_y = np.array(test_y)




###MLP
with mf.Graph().as_default():

    x = mf.placeholder()
    y_ = mf.placeholder()

    input_shape = train_X.shape[1]
    hidden_layer_1 = 256
    hidden_layer_2 = 128
    n_classes = 10

    w1 = mf.Variable(mf.random_normal([input_shape,hidden_layer_1], mu=0.0, sigma=0.1), name = 'w1')
    b1 = mf.Variable(mf.random_normal([hidden_layer_1], mu=0.0, sigma=0.1), name = 'b1')
    y1 = mf.relu(mf.matmul(x,w1)+b1)
    #y1 = batch_average(y1)
    w2 = mf.Variable(mf.random_normal([hidden_layer_1, hidden_layer_2], mu=0.0, sigma=0.1), name = 'w2')
    b2 = mf.Variable(mf.random_normal([hidden_layer_2], mu=0.0, sigma=0.1), name = 'b2')
    y2 = mf.relu(mf.matmul(y1,w2)+b2)
    #y2 = batch_average(y2)