Exemplo n.º 1
0

# Create the model
x = tf.placeholder(tf.float32, [None, NUM_FEATURES])
y_ = tf.placeholder(tf.float32, [None, NUM_CLASSES])

# Build the graph for the deep net

hidden_layer = layers.dense(x, 10, activation=tf.nn.relu, kernel_initializer=init.orthogonal(
    np.sqrt(2)), bias_initializer=init.zeros(), kernel_regularizer=l2_regularizer(1e-6))
output_layer = layers.dense(hidden_layer, 6, kernel_initializer=init.orthogonal(
), bias_initializer=init.zeros(),  kernel_regularizer=l2_regularizer(1e-6))

cross_entropy = tf.nn.softmax_cross_entropy_with_logits_v2(
    labels=y_, logits=output_layer)
loss = tf.reduce_mean(cross_entropy) + get_regularization_loss()


# Create the gradient descent optimizer with the given learning rate.
optimizer = tf.train.GradientDescentOptimizer(learning_rate)
train_op = optimizer.minimize(loss)

correct_prediction = tf.cast(
    tf.equal(tf.argmax(output_layer, 1), tf.argmax(y_, 1)), tf.float32)
accuracy = tf.reduce_mean(correct_prediction)

with tf.Session() as sess:
    sess.run(tf.global_variables_initializer())
    train_err = []
    test_acc = []
    for i in range(epochs):
Exemplo n.º 2
0
y_ = tf.placeholder(tf.float32, [None, 1])

# Build the graph for the deep net
hidden_layer = layers.dense(x,
                            30,
                            activation=tf.nn.relu,
                            kernel_initializer=init.orthogonal(np.sqrt(2)),
                            bias_initializer=init.zeros(),
                            kernel_regularizer=l2_regularizer(1e-3))
output_layer = layers.dense(hidden_layer,
                            1,
                            kernel_initializer=init.orthogonal(),
                            bias_initializer=init.zeros(),
                            kernel_regularizer=l2_regularizer(1e-3))

loss = tf.reduce_mean(tf.square(y_ - output_layer)) + get_regularization_loss()

#Create the gradient descent optimizer with the given learning rate.
optimizer = tf.train.GradientDescentOptimizer(learning_rate)
train_op = optimizer.minimize(loss)
error = tf.reduce_mean(tf.square(y_ - output_layer))

with tf.Session() as sess:
    sess.run(tf.global_variables_initializer())
    train_err = []
    for i in range(epochs):
        idm = 0
        while idm < 1000:
            nidx = idm + 32
            train_op.run(feed_dict={x: trainX[idm:nidx], y_: trainY[idm:nidx]})
            idm = nidx
    tf.reset_default_graph()
    with tf.Session() as sess:

        # Create the model
        lr = tf.placeholder(tf.float32, [])
        x = tf.placeholder(tf.float32, [None, NUM_FEATURES])
        y_ = tf.placeholder(tf.float32, [None, 1])

        # Build the graph for the deep net
        hidden_layer = layers.dense(x, neuron_count, activation=tf.nn.relu, kernel_initializer=init.orthogonal(np.sqrt(2)),
                                    bias_initializer=init.zeros(), kernel_regularizer=l2_regularizer(1e-3))
        output_layer = layers.dense(hidden_layer, 1, kernel_initializer=init.orthogonal(), bias_initializer=init.zeros(),
                                    kernel_regularizer=l2_regularizer(1e-3))

        loss = tf.reduce_mean(tf.square(y_ - output_layer)
                              ) + get_regularization_loss()

        # Create the gradient descent optimizer with the given learning rate.
        optimizer = tf.train.GradientDescentOptimizer(lr)
        train_op = optimizer.minimize(loss)
        error = tf.reduce_mean(tf.square(y_ - output_layer))

        fold_errors = []
        for o in range(NUM_FOLDS):
            sess.run(tf.global_variables_initializer())

            xTrainX = np.split(trainX, NUM_FOLDS, axis=0)
            xTrainY = np.split(trainY, NUM_FOLDS, axis=0)

            xValidationX = xTrainX[o]
            xValidationY = xTrainY[o]