gpt4 book ai didi

python - Tensorflow 不开始训练

转载 作者:行者123 更新时间:2023-11-30 09:51:21 25 4
gpt4 key购买 nike

我已按照评估 MNIST tutorial 进行操作并希望对其进行调整以使用我自己的数据集。使用初始模型,我使用 build_image_data.py 将图像转换为张量并加载它们。然后我尝试使用它们作为模型的输入,但执行会立即停止,直到 model.fit() 函数为止。此后没有 CPU 使用,也没有任何输出。

相关代码如下:

from __future__ import absolute_import
from __future__ import division
from __future__ import print_function

import numpy as np
import tensorflow as tf

from tensorflow.contrib import learn
from tensorflow.contrib.learn.python.learn.estimators import model_fn as model_fn_lib

import image_processing
import dataset

tf.logging.set_verbosity(tf.logging.INFO)

height = 200
width = 200

def cnn_model_fn(features, labels, mode):
input_layer = tf.reshape(features, [-1, width, height, 1])

con
v1 = tf.layers.conv2d(inputs=input_layer, filters=32, kernel_size=[5, 5], padding="same", activation=tf.nn.relu)
pool1 = tf.layers.max_pooling2d(inputs=conv1, pool_size=[2, 2], strides=2)
conv2 = tf.layers.conv2d(inputs=pool1, filters=64, kernel_size=[5, 5], padding="same", activation=tf.nn.relu)
pool2 = tf.layers.max_pooling2d(inputs=conv2, pool_size=[2, 2], strides=2)
pool2_flat = tf.reshape(pool2, [-1, (width/4) * (width/4) * 64])
dense = tf.layers.dense(inputs=pool2_flat, units=1024, activation=tf.nn.relu)
dropout = tf.layers.dropout(inputs=dense, rate=0.4, training=mode == learn.ModeKeys.TRAIN)
logits = tf.layers.dense(inputs=dropout, units=2)

loss = None
train_op = None

if mode != learn.ModeKeys.INFER:
onehot_labels = tf.one_hot(indices=tf.cast(labels, tf.int32), depth=2)
loss = tf.losses.softmax_cross_entropy(onehot_labels=onehot_labels, logits=logits)

if mode == learn.ModeKeys.TRAIN:
train_op = tf.contrib.layers.optimize_loss(loss=loss, global_step=tf.contrib.framework.get_global_step(), learning_rate=0.001, optimizer="SGD")

predictions = {
"classes": tf.argmax(input=logits, axis=1),
"probabilities": tf.nn.softmax(logits, name="softmax_tensor")
}

return model_fn_lib.ModelFnOps(mode=mode, predictions=predictions, loss=loss, train_op=train_op)

def main(unused_argv):
training_data = dataset.Dataset("train-00000-of-00001", "train")
validation_data = dataset.Dataset("validation-00000-of-00001", "validation")
images, labels = image_processing.inputs(training_data)
vimages, vlabels = image_processing.inputs(validation_data)

sess = tf.InteractiveSession()
feature_classifier = learn.SKCompat(learn.Estimator(model_fn=cnn_model_fn, model_dir="/tmp/feature_model"))
tensors_to_log = {"probabilities": "softmax_tensor"}
logging_hook = tf.train.LoggingTensorHook(tensors=tensors_to_log, every_n_iter=10)
feature_classifier.fit(x=images.eval(), y=labels.eval(), batch_size=100, steps=200000, monitors=[logging_hook])
metrics = {
"accuracy":
learn.MetricSpec(metric_fn=tf.metrics.accuracy, prediction_key="classes"),
}
# Evaluate the model and print results
eval_results = feature_classifier.evaluate(x=vimages.eval(), y=vlabels.eval(), metrics=metrics)
print(eval_results)

if __name__ == "__main__":
tf.app.run()

它一开始给出的唯一输出是:

INFO:tensorflow:Using default config. INFO:tensorflow:Using config: {'_save_checkpoints_steps': None, '_tf_config': gpu_options { per_process_gpu_memory_fraction: 1 } , '_tf_random_seed': None, '_keep_checkpoint_max': 5, '_num_ps_replicas': 0, '_master': '', '_is_chief': True, '_keep_checkpoint_every_n_hours': 10000, '_task_id': 0, '_save_summary_steps': 100, '_task_type': None, '_num_worker_replicas': 0, '_save_checkpoints_secs': 600, '_evaluation_master': '', '_cluster_spec': , '_environment': 'local', '_model_dir': None}

我的数据集约为 31 MB + 6 MB 用于输入和验证集。

最佳答案

您需要启动队列运行程序。以下代码更改应该有效:

sess = tf.InteractiveSession()

sess.run(tf.global_variables_initializer())
coordinator = tf.train.Coordinator()
threads = tf.train.start_queue_runners(sess=sess,coord=coordinator)

feature_classifier = learn.SKCompat(learn.Estimator(model_fn=cnn_model_fn, model_dir="/tmp/feature_model"))
...

print(eval_results)
coordinator.request_stop()
coordinator.join(threads)

另一种推荐的方法是通过进行以下更改来使用更新的估算器“input_fn”方法:

sess = tf.InteractiveSession()

feature_classifier = learn.Estimator(model_fn=cnn_model_fn, model_dir="/tmp/feature_model")
tensors_to_log = {"probabilities": "softmax_tensor"}
logging_hook = tf.train.LoggingTensorHook(tensors=tensors_to_log, every_n_iter=10)
feature_classifier.fit( input_fn=lambda:image_processing.inputs(training_data), train=True), steps=200000, monitors=[logging_hook])
metrics = {
"accuracy":
learn.MetricSpec(metric_fn=tf.metrics.accuracy, prediction_key="classes"),
}

关于python - Tensorflow 不开始训练,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44639487/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com