Ejemplo 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)))
Ejemplo 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}))
Ejemplo 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}))
Ejemplo n.º 4
0
def main():
    sess = tf.Session()

    # Add
    a = tf.constant(32.0)
    b = tf.constant(10.0)
    c = a + b
    print(sess.run(c))  # Should be 42.0

    # Minus
    c = a - b
    print(sess.run(c))  # Should be 22.0

    # Multiple
    c = a * b
    print(sess.run(c))  # Should be 320.0

    # Divide
    c = a / b
    print(sess.run(c))  # Should 3.2
Ejemplo n.º 5
0
    y2 = mf.relu(mf.matmul(y1,w2)+b2)
    #y2 = batch_average(y2)
    w3 = mf.Variable(mf.random_normal([hidden_layer_2, n_classes], mu=0.0, sigma=0.1), name = 'w3')
    b3 = mf.Variable(mf.random_normal([n_classes], mu=0.0, sigma=0.1), name = 'b3')
    y3 = mf.relu(mf.matmul(y2,w3)+b3)
    #y3 = batch_average(y3)
    loss = mf.reduce_sum(mf.square(y_-y3))
    train_op = mf.GradientDescentOptimizer(learning_rate=0.005).minimize(loss)
    #train_op = mf.ExponentialDecay(learning_rate=0.01, decay_rate=0.01).minimize(loss)
    train_y = mf.onehot_encoding(train_y, 10)#normalization(train_y,10)
    test_y = mf.onehot_encoding(test_y, 10)#normalization(test_y,10)
    #feed_dict = {x:train_X, y_:normalization(train_y,10)}
    #eval = equal(argmax(y3,0),argmax(y_,0))
    accurate = mf.equal(mf.argmax(y3,1), mf.argmax(y_,1))

with mf.Session() as sess:
    epoches = 3
    batch_size = 300
    batches = int(len(train_X)/batch_size)
    #remains = len(train_X) - batches*batch_size

    for step in range(epoches):
        for batch in range(batches):
            loss_value = 0
            accuracy = 0
            mse = 0
            start = batch*batch_size
            end = (batch+1)*batch_size
            #for index in range(start,end):
            X = train_X[start:end]
            Y = train_y[start:end]