首页 > 解决方案 > 如何从张量流模型中正确访问变量和操作?

问题描述

我正在使用 tensorflow-gpu 1.8.0 来训练\验证 MLP。另外,我正在使用 Hyperopt 来寻找其最佳配置。下面的代码似乎按预期工作:

def train_network_1(参数):

n_step= np.round(parameters['step'],3)
n_hidden= np.int(parameters['number_neurons'])
n_bias= np.round(parameters['bias'],3)
n_batch= np.int(parameters['batch'])

# General variables
N_instances= xtrain_data_1_T60.shape[0]
N_input= xtrain_data_1_T60.shape[1]
N_classes= enc_ytrain_data_1_T60.shape[1]
N_epochs= 500
display_step= 100

tf.reset_default_graph()

# Placeholders
X= tf.placeholder(name= "Logs", dtype= tf.float32, shape= [None, N_input])
y= tf.placeholder(name= "Facies", dtype= tf.float32, shape= [None, N_classes])

# MLP network architecture
def _MLP(_X, _weights, _biases):
    # Input layer
    input_layer= tf.add(tf.matmul(_X, _weights['input_']), _biases['input'])

    # Hidden layer
    hidden_layer= tf.nn.tanh(tf.add(tf.matmul(input_layer, _weights['hidden_']), _biases['hidden']))

    # Output layer
    output_layer= tf.nn.softmax(tf.add(tf.matmul(hidden_layer, _weights['output_']), _biases['output']))

    return output_layer

weights= {'input_': tf.get_variable(name= "A_input", shape= [N_input, N_input], initializer= tf.keras.initializers.glorot_normal(1969),
                                   dtype= tf.float32),
          'hidden_': tf.get_variable(name= "B_hidden", shape= [N_input, n_hidden], initializer= tf.keras.initializers.he_normal(1969),
                                    dtype= tf.float32),
          'output_': tf.get_variable(name= "C_output", shape= [n_hidden, N_classes], initializer= tf.keras.initializers.he_normal(1969),
                                    dtype= tf.float32),
        }

biases= {'input': tf.Variable(tf.zeros([N_input]), dtype= tf.float32),
         'hidden': tf.Variable(tf.scalar_mul(tf.constant(n_bias, dtype= tf.float32), tf.ones([n_hidden])), dtype= tf.float32),
         'output': tf.Variable(tf.zeros([N_classes]), dtype= tf.float32),
         }

# Construct model
with tf.variable_scope('Model'):
    model= _MLP(X, weights, biases)

# Define loss and optimizer
with tf.variable_scope('Optimizer'):
    loss_op= tf.reduce_mean(tf.keras.backend.binary_crossentropy(y, model))
    optimizer= tf.train.GradientDescentOptimizer(learning_rate= n_step).minimize(loss_op)

# Initialize variables
init= tf.global_variables_initializer() #tf.initialize_all_variables()

# Initialize a saver to save the current best model
saver = tf.train.Saver(max_to_keep = 1)

with tf.Session() as sess:
    sess.run(init)

    # Initialize a saver to save the current best model
    #saver = tf.train.Saver(max_to_keep = 1)

    # Training loop
    for epoch in range(0, N_epochs):
        avg_cost = 0.
        total_batch= np.int(N_instances/n_batch)
        start_idx= 0
        end_idx= n_batch

        for i in range(0, total_batch):
            batchx= xtrain_data_1_T60[start_idx:end_idx,:]
            batchy= enc_ytrain_data_1_T60[start_idx:end_idx,:]

            _, c= sess.run([optimizer, loss_op], feed_dict= {X: batchx, y: batchy})
            avg_cost += c/total_batch

            # Set next batch
            start_idx += n_batch
            end_idx += n_batch
            if (end_idx > N_instances):
                end_idx= N_instances

        if (epoch % display_step == 0):
            print("Epoch : %03d/%03d cost : %.4f\n"%(epoch, N_epochs, avg_cost))

    print("Optimization finished\n")

    prediction_1= sess.run(model, feed_dict= {X: xvalidation_data_1_V40})
    prediction_1= prediction_1.argmax(axis= 1) + 1

    # Only check for prediction results with 3 lithofacies. Otherwise, I assign a dummy error and accuracy
    if len(np.unique(prediction_1)) == 3:
        error= 1. - metrics.recall_score(yvalidation_data_1_V40, prediction_1, average= 'micro')
        accuracy= metrics.accuracy_score(yvalidation_data_1_V40, prediction_1)

        global temp_error
        if (error < temp_error):
            temp_error= error
            saver.save(sess, '{}/{}'.format(checkpoint_path, checkpoint_name))
            print("Best model saved in file: ", '{}/{}'.format(checkpoint_path, checkpoint_name))

    else:
        error= 3
        accuracy= 0.00

    print("Error: {}".format(error))
    print("Accuracy: {:.2%}".format(accuracy))
    print("Predicted number of lithofacies: {}\n".format(len(np.unique(prediction_1))))

return {'loss': error,
        'n_prediction': prediction_1,
        'n_step': n_step,
        'n_hidden': n_hidden,
        'n_bias': n_bias,
        'n_accuracy': accuracy,
        'n_batch': n_batch,
        'status': STATUS_OK}

随后,在同一个脚本中,恢复了最佳训练的 MLP 模型。这是发生问题的部分。我想重用经过训练的模型(名为“模型”的操作)来预测新数据。为此:

检查点= tf.train.latest_checkpoint(checkpoint_path + "/")

使用 tf.Session() 作为 sess:

new_saver= tf.train.import_meta_graph(checkpoint + ".meta")
graph= tf.get_default_graph()
sess.run(tf.global_variables_initializer())

X= graph.get_tensor_by_name('Logs:0') 
print(X)  -> this confirms that I found the correct variable

model_prediction= sess.run('Model/MatMul', feed_dict={X: voting_data})

从最后一个命令中,我收到以下错误消息:

InvalidArgumentError(有关回溯,请参见上文):您必须为占位符张量“Logs”提供一个值,其 dtype 为 float 和 shape [?,4] [[Node: Logs = Placeholderdtype=DT_FLOAT, shape=[?,4], _device=" /job:localhost/replica:0/task:0/device:GPU:0"]]

但是,输入数据 (voting_data) 具有正确的形状 (255,4)

有什么建议么?

非常感谢,

伊万

标签: pythontensorflowplaceholderrestoreprediction

解决方案


推荐阅读