我正在使用MorphNet Python库针对使用TensorFlow创建的Softmax回归优化神经网络。 使用MNIST数据集作为输入,将Softmax回归用于数字识别。
Morphnet库是通过以下链接安装的:
https://github.com/google-research/morph-net
使用MorphNet库进行Softmax回归和神经网络优化的TensorFlow 代码如下所示:
import tensorflow as tf
import numpy as np
import matplotlib.pyplot as plt
from tensorflow.examples.tutorials.mnist import input_data
from morph_net.network_regularizers import flop_regularizer
from morph_net.tools import structure_exporter
mnist = input_data.read_data_sets("MNIST_data/", one_hot=True)
print("Shape of feature matrix:", mnist.train.images.shape)
print("Shape of target matrix:", mnist.train.labels.shape)
print("One-hot encoding for 1st observation:\n", mnist.train.labels[0])
# number of features
num_features = 784
# number of target labels
num_labels = 10
# learning rate (alpha)
learning_rate = 0.05
# batch size
batch_size = 128
# number of epochs
num_steps = 5001
# input data
train_dataset = mnist.train.images
train_labels = mnist.train.labels
test_dataset = mnist.test.images
test_labels = mnist.test.labels
valid_dataset = mnist.validation.images
valid_labels = mnist.validation.labels
# initialize a tensorflow graph
graph = tf.Graph()
morphnet_max_steps = 10
# with graph.as_default():
with tf.Session() as sess:
"""
defining all the nodes
"""
# Inputs
tf_train_dataset = tf.placeholder(tf.float32, shape=(batch_size, num_features))
tf_train_labels = tf.placeholder(tf.float32, shape=(batch_size, num_labels))
tf_valid_dataset = tf.constant(valid_dataset)
tf_test_dataset = tf.constant(test_dataset)
# Variables.
weights = tf.Variable(tf.truncated_normal([num_features, num_labels]))
biases = tf.Variable(tf.zeros([num_labels]))
# Training computation.
logits = tf.matmul(tf_train_dataset, weights) + biases
loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(
labels=tf_train_labels, logits=logits))
# Optimizer.
optimizer = tf.train.GradientDescentOptimizer(learning_rate).minimize(loss)
# Predictions for the training, validation, and test data.
train_prediction = tf.nn.softmax(logits)
valid_prediction = tf.nn.softmax(tf.matmul(tf_valid_dataset, weights) + biases)
test_prediction = tf.nn.softmax(tf.matmul(tf_test_dataset, weights) + biases)
'''
Morphnet
'''
network_regularizer = flop_regularizer.GammaFlopsRegularizer(
[logits.op], gamma_threshold=1e-3)
regularization_strength = 1e-10
regularizer_loss = (network_regularizer.get_regularization_term() * regularization_strength)
model_loss = tf.nn.softmax_cross_entropy_with_logits(
labels=tf_train_labels, logits=logits)
momentum_optimizer = tf.train.MomentumOptimizer(learning_rate=0.01, momentum=0.9)
train_op = momentum_optimizer.minimize(model_loss + regularizer_loss)
tf.summary.scalar('RegularizationLoss', regularizer_loss)
tf.summary.scalar(network_regularizer.cost_name, network_regularizer.get_cost())
exporter = structure_exporter.StructureExporter(
network_regularizer.op_regularizer_manager)
for step in range(morphnet_max_steps):
# sess = tf.Session(graph=tf.get_default_graph())
_, structure_exporter_tensors = sess.run([train_op, exporter.tensors])
if (step % 1000 == 0):
exporter.populate_tensor_values(structure_exporter_tensors)
exporter.create_file_and_save_alive_counts("train_dir", step)
运行上述代码时,出现以下错误:
Traceback (most recent call last):
File "C:\Users\user1\Anaconda3\lib\site-packages\tensorflow\python\client\session.py", line 1350, in _do_call
return fn(*args)
File "C:\Users\user1\Anaconda3\lib\site-packages\tensorflow\python\client\session.py", line 1329, in _run_fn
status, run_metadata)
File "C:\Users\user1\Anaconda3\lib\site-packages\tensorflow\python\framework\errors_impl.py", line 473, in __exit__
c_api.TF_GetCode(self.status.status))
tensorflow.python.framework.errors_impl.InvalidArgumentError: You must feed a value for placeholder tensor 'Placeholder_1' with dtype float and shape [128,10]
[[Node: Placeholder_1 = Placeholder[dtype=DT_FLOAT, shape=[128,10], _device="/job:localhost/replica:0/task:0/device:CPU:0"]()]]
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "C:/Users/user1/PycharmProjects/Project-Morphnet-Demo/file1.py", line 89, in <module>
_, structure_exporter_tensors = sess.run([train_op, exporter.tensors])
File "C:\Users\user1\Anaconda3\lib\site-packages\tensorflow\python\client\session.py", line 895, in run
run_metadata_ptr)
File "C:\Users\user1\Anaconda3\lib\site-packages\tensorflow\python\client\session.py", line 1128, in _run
feed_dict_tensor, options, run_metadata)
File "C:\Users\user1\Anaconda3\lib\site-packages\tensorflow\python\client\session.py", line 1344, in _do_run
options, run_metadata)
File "C:\Users\user1\Anaconda3\lib\site-packages\tensorflow\python\client\session.py", line 1363, in _do_call
raise type(e)(node_def, op, message)
tensorflow.python.framework.errors_impl.InvalidArgumentError: You must feed a value for placeholder tensor 'Placeholder_1' with dtype float and shape [128,10]
[[Node: Placeholder_1 = Placeholder[dtype=DT_FLOAT, shape=[128,10], _device="/job:localhost/replica:0/task:0/device:CPU:0"]()]]
Caused by op 'Placeholder_1', defined at:
File "C:/Users/user1/PycharmProjects/Project-Morphnet-Demo/file1.py", line 46, in <module>
tf_train_labels = tf.placeholder(tf.float32, shape=(batch_size, num_labels))
File "C:\Users\user1\Anaconda3\lib\site-packages\tensorflow\python\ops\array_ops.py", line 1680, in placeholder
return gen_array_ops._placeholder(dtype=dtype, shape=shape, name=name)
File "C:\Users\user1\Anaconda3\lib\site-packages\tensorflow\python\ops\gen_array_ops.py", line 4105, in _placeholder
"Placeholder", dtype=dtype, shape=shape, name=name)
File "C:\Users\user1\Anaconda3\lib\site-packages\tensorflow\python\framework\op_def_library.py", line 787, in _apply_op_helper
op_def=op_def)
File "C:\Users\user1\Anaconda3\lib\site-packages\tensorflow\python\framework\ops.py", line 3160, in create_op
op_def=op_def)
File "C:\Users\user1\Anaconda3\lib\site-packages\tensorflow\python\framework\ops.py", line 1625, in __init__
self._traceback = self._graph._extract_stack() # pylint: disable=protected-access
InvalidArgumentError (see above for traceback): You must feed a value for placeholder tensor 'Placeholder_1' with dtype float and shape [128,10]
[[Node: Placeholder_1 = Placeholder[dtype=DT_FLOAT, shape=[128,10], _device="/job:localhost/replica:0/task:0/device:CPU:0"]()]]
如何解决上述错误,并使MorphNet在TensorFlow中用于Softmax回归?