Python cifar10自动编码器出现错误:必须为占位符张量';x';使用dtype float
我尝试用cifar10构建隐私自动编码器,我参考了Magnus Erik Hvass Pedersen()为cnn编写的教程。 这是我的代码:Python cifar10自动编码器出现错误:必须为占位符张量';x';使用dtype float,python,tensorflow,Python,Tensorflow,我尝试用cifar10构建隐私自动编码器,我参考了Magnus Erik Hvass Pedersen()为cnn编写的教程。 这是我的代码: import matplotlib.pyplot as plt import tensorflow as tf import numpy as np from sklearn.metrics import confusion_matrix import time from datetime import timedelta import math imp
import matplotlib.pyplot as plt
import tensorflow as tf
import numpy as np
from sklearn.metrics import confusion_matrix
import time
from datetime import timedelta
import math
import os
import cifar10
from cifar10 import img_size, num_channels, num_classes
gpu_options = tf.GPUOptions(per_process_gpu_memory_fraction=0.03)
session = tf.Session(config=tf.ConfigProto(gpu_options=gpu_options))
cifar10.maybe_download_and_extract()
class_names = cifar10.load_class_names()
images_train, cls_train, labels_train = cifar10.load_training_data()
images_test, cls_test, labels_test = cifar10.load_test_data()
print("Size of:")
print("- Training-set:\t\t{}".format(len(images_train)))
print("- Test-set:\t\t{}".format(len(images_test)))
img_size_cropped = 24
x = tf.placeholder(tf.float32, shape=[None, img_size, img_size, num_channels], name='x')
y_true = tf.placeholder(tf.float32, shape=[None, num_classes], name='y_true')
y_true_cls = tf.argmax(y_true, dimension=1)
def pre_process_image(image, training):
# This function takes a single image as input,
# and a boolean whether to build the training or testing graph.
if training:
# For training, add the following to the TensorFlow graph.
# Randomly crop the input image.
image = tf.random_crop(image, size=[img_size_cropped, img_size_cropped, num_channels])
# Randomly flip the image horizontally.
image = tf.image.random_flip_left_right(image)
# Randomly adjust hue, contrast and saturation.
image = tf.image.random_hue(image, max_delta=0.05)
image = tf.image.random_contrast(image, lower=0.3, upper=1.0)
image = tf.image.random_brightness(image, max_delta=0.2)
image = tf.image.random_saturation(image, lower=0.0, upper=2.0)
# Some of these functions may overflow and result in pixel
# values beyond the [0, 1] range. It is unclear from the
# documentation of TensorFlow 0.10.0rc0 whether this is
# intended. A simple solution is to limit the range.
# Limit the image pixels between [0, 1] in case of overflow.
image = tf.minimum(image, 1.0)
image = tf.maximum(image, 0.0)
else:
# For training, add the following to the TensorFlow graph.
# Crop the input image around the centre so it is the same
# size as images that are randomly cropped during training.
image = tf.image.resize_image_with_crop_or_pad(image,
target_height=img_size_cropped,
target_width=img_size_cropped)
return image
def pre_process(images, training):
# Use TensorFlow to loop over all the input images and call
# the function above which takes a single image as input.
images = tf.map_fn(lambda image: pre_process_image(image, training), images)
return images
#define the first privacy autoencoder parameters
n_input=img_size_cropped*img_size_cropped*num_channels #24*24*3
n_hidden_1=math.floor(img_size_cropped*img_size_cropped*num_channels/2)
weights={
'encoder_h1':tf.Variable(tf.truncated_normal([n_input,n_hidden_1], stddev=1/192.0)),
'decoder_h1':tf.Variable(tf.truncated_normal([n_hidden_1,n_input], stddev=1/192.0)),
}
biases={
'encoder_b1':tf.Variable(tf.random_normal([n_hidden_1])),
'decoder_b1':tf.Variable(tf.random_normal([n_input])),
}
epsilon = 0.0005
train_batch_size = 128
batch_size = 128
def inference(images):
xs=tf.reshape(images,[tf.shape(x)[0], img_size_cropped*img_size_cropped*num_channels])
#encoder
h=tf.add(tf.matmul(xs, weights['encoder_h1']), biases['encoder_b1'])
encoder_layer_1=tf.nn.sigmoid(h)
#decoder
x_tilde=tf.add(tf.matmul(encoder_layer_1,weights['decoder_h1']), biases['decoder_b1'])
decoder_layer_1=tf.nn.sigmoid(x_tilde)
#encoder_layer_1 for next dPA use, decoder_layer_1 for decoder_output, x_tilde for objective function( loss_restruction() )
return encoder_layer_1, decoder_layer_1, x_tilde
def loss_restruction(images,decoder_layer_1, x_tilde):
xs=tf.reshape(images,[tf.shape(x)[0], img_size_cropped*img_size_cropped*num_channels])
y_pred=decoder_layer_1
f1=tf.multiply(xs,math.log(2)) #shape=(128, 1728)
f2=tf.multiply((tf.ones_like(xs)-xs),math.log(2)) #shape=(128,1728)
#difine polynomial coefficients to stuck laplacian noise
coefficient_1=tf.add(f1,f2)
coefficient_2=tf.add(f1,f2)/1
coefficient_3=tf.add(f1,f2)/(2*1)
#compute global sensitivity to generate laplacian noise
global_sensitivity=2.0*tf.reduce_max(
tf.reduce_sum(tf.add(tf.add(tf.abs(coefficient_1), tf.abs(coefficient_2)),tf.abs(coefficient_3)),axis=1, keep_dims=True),reduction_indices=0) #[1].*2
sen=session.run(global_sensitivity)
lap_noise1=np.random.laplace(loc=0.0, scale=(sen[0]/epsilon), size=(tf.shape(x)[0], img_size_cropped*img_size_cropped*num_channels))
lap_noise2=np.random.laplace(loc=0.0, scale=(sen[0]/epsilon), size=(tf.shape(x)[0], img_size_cropped*img_size_cropped*num_channels))
lap_noise3=np.random.laplace(loc=0.0, scale=(sen[0]/epsilon), size=(tf.shape(x)[0], img_size_cropped*img_size_cropped*num_channels))
#restruction function with laplacian noise as a loss function
loss=tf.reduce_mean(tf.reduce_sum(tf.add(
tf.add(tf.add(coefficient_1,lap_noise1), tf.multiply(tf.add(coefficient_2,lap_noise2), x_tilde)),
tf.multiply(tf.add(coefficient_3,lap_noise3), tf.square(x_tilde)) ),1))
return y_pred, loss
'''
Helper-function for creating Main Processing
The following helper-function creates the main part of the privacy autoencoder.
'''
def main_network(images, training):
encoder_layer_1, decoder_layer_1, x_tilde=inference(images)
if training:
y_pred, loss=loss_restruction(images, decoder_layer_1, x_tilde)
else:
y_pred=decoder_layer_1
loss=tf.constant([0])
return y_pred, loss
def create_network(training):
with tf.variable_scope('network', reuse=not training):
# Just rename the input placeholder variable for convenience.
images = x
# Create TensorFlow graph for pre-processing.
images = pre_process(images=images, training=training)
# Create TensorFlow graph for the main processing.
y_pred, loss = main_network(images=images, training=training)
return y_pred, loss
global_step = tf.Variable(initial_value=0, name='global_step', trainable=False)
_, loss = create_network(training=True)
optimizer = tf.train.GradientDescentOptimizer(learning_rate=0.001).minimize(loss, global_step=global_step)
y_pred, _ = create_network(training=False)
saver = tf.train.Saver()
save_dir = 'checkpoints/'
if not os.path.exists(save_dir):
os.makedirs(save_dir)
save_path = os.path.join(save_dir, 'cifar10_privacy_autoencoder')
try:
print("Trying to restore last checkpoint ...")
last_chk_path = tf.train.latest_checkpoint(checkpoint_dir=save_dir)
saver.restore(session, save_path=last_chk_path)
print("Restored checkpoint from:", last_chk_path)
except:
print("Failed to restore checkpoint. Initializing variables instead.")
session.run(tf.global_variables_initializer())
'''
Function for selecting a random batch of images from the training-set.
'''
def random_batch():
# Number of images in the training-set.
num_images = len(images_train)
# Create a random index.
idx = np.random.choice(num_images,
size=train_batch_size,
replace=False)
# Use the random index to select random images and labels.
x_batch = images_train[idx, :, :, :]
y_batch = labels_train[idx, :]
return x_batch, y_batch
def optimize(num_iterations):
# Start-time used for printing time-usage below.
start_time = time.time()
for i in range(num_iterations):
x_batch, y_true_batch = random_batch()
feed_dict_train = {x: x_batch,
y_true: y_true_batch}
i_global, _, cost = session.run([global_step, optimizer, loss],
feed_dict=feed_dict_train)
# Print status to screen every 100 iterations (and last).
if (i_global % 100 == 0) or (i == num_iterations - 1):
# Print status.
msg = "Global Step: {0:>6}, Training Batch Cost: {1:>6.1%}"
print(msg.format(i_global, cost))
# Save a checkpoint to disk every 1000 iterations (and last).
if (i_global % 1000 == 0) or (i == num_iterations - 1):
saver.save(session,
save_path=save_path,
global_step=global_step)
print("Saved checkpoint.")
# Ending time.
end_time = time.time()
# Difference between start and end-times.
time_dif = end_time - start_time
# Print the time-usage.
print("Time usage: " + str(timedelta(seconds=int(round(time_dif)))))
print("Optimization Finished!")
optimize(num_iterations=2000)
不幸的是,我在计算损失函数的全局灵敏度时收到一条错误消息:
lap_noise1=np.random.laplace(loc=0.0, scale=(sen[0]/epsilon), size=(tf.shape(x)[0], img_size_cropped*img_size_cropped*num_channels))
错误消息:
InvalidArgumentError (see above for traceback): You must feed a value for placeholder tensor 'x' with dtype float
详细的错误消息如下所示:
Loading data: /tmp/cifar10_data\cifar-10-batches-py/data_batch_2
Loading data: /tmp/cifar10_data\cifar-10-batches-py/data_batch_3
Loading data: /tmp/cifar10_data\cifar-10-batches-py/data_batch_4
Loading data: /tmp/cifar10_data\cifar-10-batches-py/data_batch_5
Loading data: /tmp/cifar10_data\cifar-10-batches-py/test_batch
Size of:
- Training-set: 50000
- Test-set: 10000
E c:\tf_jenkins\home\workspace\release-win\device\cpu\os\windows\tensorflow\core\framework\op_kernel.cc:943] OpKernel ('op: "BestSplits" device_type: "CPU"') for unknown op: BestSplits
E c:\tf_jenkins\home\workspace\release-win\device\cpu\os\windows\tensorflow\core\framework\op_kernel.cc:943] OpKernel ('op: "CountExtremelyRandomStats" device_type: "CPU"') for unknown op: CountExtremelyRandomStats
E c:\tf_jenkins\home\workspace\release-win\device\cpu\os\windows\tensorflow\core\framework\op_kernel.cc:943] OpKernel ('op: "FinishedNodes" device_type: "CPU"') for unknown op: FinishedNodes
E c:\tf_jenkins\home\workspace\release-win\device\cpu\os\windows\tensorflow\core\framework\op_kernel.cc:943] OpKernel ('op: "GrowTree" device_type: "CPU"') for unknown op: GrowTree
E c:\tf_jenkins\home\workspace\release-win\device\cpu\os\windows\tensorflow\core\framework\op_kernel.cc:943] OpKernel ('op: "ReinterpretStringToFloat" device_type: "CPU"') for unknown op: ReinterpretStringToFloat
E c:\tf_jenkins\home\workspace\release-win\device\cpu\os\windows\tensorflow\core\framework\op_kernel.cc:943] OpKernel ('op: "SampleInputs" device_type: "CPU"') for unknown op: SampleInputs
E c:\tf_jenkins\home\workspace\release-win\device\cpu\os\windows\tensorflow\core\framework\op_kernel.cc:943] OpKernel ('op: "ScatterAddNdim" device_type: "CPU"') for unknown op: ScatterAddNdim
E c:\tf_jenkins\home\workspace\release-win\device\cpu\os\windows\tensorflow\core\framework\op_kernel.cc:943] OpKernel ('op: "TopNInsert" device_type: "CPU"') for unknown op: TopNInsert
E c:\tf_jenkins\home\workspace\release-win\device\cpu\os\windows\tensorflow\core\framework\op_kernel.cc:943] OpKernel ('op: "TopNRemove" device_type: "CPU"') for unknown op: TopNRemove
E c:\tf_jenkins\home\workspace\release-win\device\cpu\os\windows\tensorflow\core\framework\op_kernel.cc:943] OpKernel ('op: "TreePredictions" device_type: "CPU"') for unknown op: TreePredictions
E c:\tf_jenkins\home\workspace\release-win\device\cpu\os\windows\tensorflow\core\framework\op_kernel.cc:943] OpKernel ('op: "UpdateFertileSlots" device_type: "CPU"') for unknown op: UpdateFertileSlots
Traceback (most recent call last):
File "C:\Users\Lee Janice\AppData\Local\Programs\Python\Python35\lib\site-packages\tensorflow\python\client\session.py", line 1022, in _do_call
return fn(*args)
File "C:\Users\Lee Janice\AppData\Local\Programs\Python\Python35\lib\site-packages\tensorflow\python\client\session.py", line 1004, in _run_fn
status, run_metadata)
File "C:\Users\Lee Janice\AppData\Local\Programs\Python\Python35\lib\contextlib.py", line 66, in __exit__
next(self.gen)
File "C:\Users\Lee Janice\AppData\Local\Programs\Python\Python35\lib\site-packages\tensorflow\python\framework\errors_impl.py", line 466, in raise_exception_on_not_ok_status
pywrap_tensorflow.TF_GetCode(status))
tensorflow.python.framework.errors_impl.InvalidArgumentError: You must feed a value for placeholder tensor 'x' with dtype float
[[Node: x = Placeholder[dtype=DT_FLOAT, shape=[], _device="/job:localhost/replica:0/task:0/cpu:0"]()]]
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "autoencoder.py", line 158, in <module>
_, loss = create_network(training=True)
File "autoencoder.py", line 153, in create_network
y_pred, loss = main_network(images=images, training=training)
File "autoencoder.py", line 136, in main_network
y_pred, loss=loss_restruction(images, decoder_layer_1, x_tilde)
File "autoencoder.py", line 120, in loss_restruction
sen=session.run(global_sensitivity)
File "C:\Users\Lee Janice\AppData\Local\Programs\Python\Python35\lib\site-packages\tensorflow\python\client\session.py", line 767, in run
run_metadata_ptr)
File "C:\Users\Lee Janice\AppData\Local\Programs\Python\Python35\lib\site-packages\tensorflow\python\client\session.py", line 965, in _run
feed_dict_string, options, run_metadata)
File "C:\Users\Lee Janice\AppData\Local\Programs\Python\Python35\lib\site-packages\tensorflow\python\client\session.py", line 1015, in _do_run
target_list, options, run_metadata)
File "C:\Users\Lee Janice\AppData\Local\Programs\Python\Python35\lib\site-packages\tensorflow\python\client\session.py", line 1035, in _do_call
raise type(e)(node_def, op, message)
tensorflow.python.framework.errors_impl.InvalidArgumentError: You must feed a value for placeholder tensor 'x' with dtype float
[[Node: x = Placeholder[dtype=DT_FLOAT, shape=[], _device="/job:localhost/replica:0/task:0/cpu:0"]()]]
Caused by op 'x', defined at:
File "autoencoder.py", line 25, in <module>
x = tf.placeholder(tf.float32, shape=[None, img_size, img_size, num_channels], name='x')
File "C:\Users\Lee Janice\AppData\Local\Programs\Python\Python35\lib\site-packages\tensorflow\python\ops\array_ops.py", line 1502, in placeholder
name=name)
File "C:\Users\Lee Janice\AppData\Local\Programs\Python\Python35\lib\site-packages\tensorflow\python\ops\gen_array_ops.py", line 2149, in _placeholder
name=name)
File "C:\Users\Lee Janice\AppData\Local\Programs\Python\Python35\lib\site-packages\tensorflow\python\framework\op_def_library.py", line 763, in apply_op
op_def=op_def)
File "C:\Users\Lee Janice\AppData\Local\Programs\Python\Python35\lib\site-packages\tensorflow\python\framework\ops.py", line 2327, in create_op
original_op=self._default_original_op, op_def=op_def)
File "C:\Users\Lee Janice\AppData\Local\Programs\Python\Python35\lib\site-packages\tensorflow\python\framework\ops.py", line 1226, in __init__
self._traceback = _extract_stack()
InvalidArgumentError (see above for traceback): You must feed a value for placeholder tensor 'x' with dtype float
[[Node: x = Placeholder[dtype=DT_FLOAT, shape=[], _device="/job:localhost/replica:0/task:0/cpu:0"]()]]
加载数据:/tmp/cifar10\u data\cifar-10-batches-py/data\u batch\u 2
加载数据:/tmp/cifar10\u data\cifar-10-batches-py/data\u batch\u 3
加载数据:/tmp/cifar10\u data\cifar-10-batches-py/data\u batch\u 4
加载数据:/tmp/cifar10\u data\cifar-10-batches-py/data\u batch\u 5
加载数据:/tmp/cifar10\u data\cifar-10-batches-py/test\u batch
尺寸:
-训练集:50000
-测试集:10000
E c:\tf_jenkins\home\workspace\release win\device\cpu\os\windows\tensorflow\core\framework\op_kernel.cc:943]OpKernel('op:'BestSplits'设备类型:'cpu'),用于未知op:BestSplits
E c:\tf_jenkins\home\workspace\release win\device\cpu\os\windows\tensorflow\core\framework\op_kernel.cc:943]OpKernel('op:'CountExtremelyRandomStats“device_type:'cpu'),用于未知op:CountExtremelyRandomStats
E c:\tf_jenkins\home\workspace\release win\device\cpu\os\windows\tensorflow\core\framework\op_kernel.cc:943]用于未知op:FinishedNodes的OpKernel('op:'FinishedNodes'设备类型:'cpu')
E c:\tf_jenkins\home\workspace\release win\device\cpu\os\windows\tensorflow\core\framework\op_kernel.cc:943]用于未知op:GrowTree的OpKernel('op:'GrowTree'设备类型:'cpu')
E c:\tf\u jenkins\home\workspace\release win\device\cpu\os\windows\tensorflow\core\framework\op\kernel.cc:943]OpKernel('op:“ReinterpretStringToFloat”设备类型:“cpu”)用于未知op:ReinterpretStringToFloat
E c:\tf_jenkins\home\workspace\release win\device\cpu\os\windows\tensorflow\core\framework\op_kernel.cc:943]OpKernel('op:“SampleInputs”device\u type:'cpu'),用于未知op:SampleInputs
E c:\tf_jenkins\home\workspace\release win\device\cpu\os\windows\tensorflow\core\framework\op_kernel.cc:943]OpKernel('op:“ScatterAddNdim”device_type:'cpu'),用于未知op:ScatterAddNdim
E c:\tf\u jenkins\home\workspace\release win\device\cpu\os\windows\tensorflow\core\framework\op\u kernel.cc:943]OpKernel('op:“TopNInsert”device\u type:'cpu'),用于未知op:TopNInsert
E c:\tf\u jenkins\home\workspace\release win\device\cpu\os\windows\tensorflow\core\framework\op\u kernel.cc:943]OpKernel('op:TopNRemove“device\u type:'cpu'),用于未知op:TopNRemove
E c:\tf_jenkins\home\workspace\release win\device\cpu\os\windows\tensorflow\core\framework\op_kernel.cc:943]OpKernel('op:“TreePredictions”device\u type:'cpu'),用于未知op:TreePredictions
E c:\tf_jenkins\home\workspace\release win\device\cpu\os\windows\tensorflow\core\framework\op_kernel.cc:943]OpKernel(“op:“UpdateFertileSlots”设备类型:“cpu”)用于未知op:UpdateFertileSlots
回溯(最近一次呼叫最后一次):
文件“C:\Users\Lee Janice\AppData\Local\Programs\Python\Python35\lib\site packages\tensorflow\Python\client\session.py”,第1022行,在调用中
返回fn(*args)
文件“C:\Users\Lee Janice\AppData\Local\Programs\Python\Python35\lib\site packages\tensorflow\Python\client\session.py”,第1004行,在\u run\u fn中
状态,运行(元数据)
文件“C:\Users\Lee Janice\AppData\Local\Programs\Python\Python35\lib\contextlib.py”,第66行,在__
下一个(self.gen)
文件“C:\Users\Lee Janice\AppData\Local\Programs\Python\Python35\lib\site packages\tensorflow\Python\framework\errors\u impl.py”,第466行,处于raise\u exception\u on\u not\u ok\u状态
pywrap_tensorflow.TF_GetCode(状态))
tensorflow.python.framework.errors\u impl.InvalidArgumentError:必须为带有dtype float的占位符tensor“x”提供一个值
[[Node:x=Placeholder[dtype=DT_FLOAT,shape=[],_device=“/job:localhost/replica:0/task:0/cpu:0”]()]
在处理上述异常期间,发生了另一个异常:
回溯(最近一次呼叫最后一次):
文件“autoencoder.py”,第158行,在
_,损失=创建网络(培训=真)
文件“autoencoder.py”,第153行,在创建_网络中
y_pred,损耗=主网络(图像=图像,训练=训练)
主网络中第136行的文件“autoencoder.py”
y_pred,损耗=损耗重构(图像,解码器层1,x_瓷砖)
文件“autoencoder.py”,第120行,丢失重建
sen=会话运行(全局_敏感度)
文件“C:\Users\Lee Janice\AppData\Local\Programs\Python\Python35\lib\site packages\tensorflow\Python\client\session.py”,第767行,正在运行
运行_元数据_ptr)
文件“C:\Users\Lee Janice\AppData\Local\Programs\Python\Python35\lib\site packages\tensorflow\Python\client\session.py”,第965行,正在运行
提要(dict字符串、选项、运行元数据)
文件“C:\Users\Lee Janice\AppData\Local\Programs\Python\Python35\lib\site packages\tensorflow\Python\client\session.py”,第1015行,在运行中
目标\u列表、选项、运行\u元数据)
文件“C:\Users\Lee Janice\AppData\Local\Programs\Python\Python35\lib\site packages\tensorflow\Python\client\session.py”,第1035行,在调用中
提升类型(e)(节点定义、操作、消息)
tensorflow.python.framework.errors\u impl.InvalidArgumentError:必须为带有dtype float的占位符tensor“x”提供一个值
[[Node:x=Placeholder[dtype=DT_FLOAT,shape=[],_device=“/job:localhost/replica:0/task:0/cpu:0”]()]
由op“x”引起,定义为:
文件“autoencoder.py”,第25行,在
x=tf.placeholder(tf.float32,shape=[None,img\u size,img\u size,num\u channels],name='x')
文件“C:\Users\Lee Janice\AppData\Local\Programs\Python\Python35\lib\site packages\tensorflow\Python\ops\array\u ops.py”,第1502行,占位符
名称=名称)
文件“C:\Users\Lee Janice\AppData\Local\Programs\Python\Python35\lib\site packages\tensorflow\Python\ops\gen\u array\u ops.py”,第2149行,在占位符中
名称=名称)
文件“C:\Users\Lee Janice\AppData\Local\Programs\Python\Python35\lib\site packages\tensorflow\Python\framework\op\u def
sen=session.run(global_sensitivity)
lap_noise1=np.random.laplace(loc=0.0, scale=(sen[0]/epsilon), size=(tf.shape(x)[0], img_size_cropped*img_size_cropped*num_channels))
lap_noise2=np.random.laplace(loc=0.0, scale=(sen[0]/epsilon), size=(tf.shape(x)[0], img_size_cropped*img_size_cropped*num_channels))
lap_noise3=np.random.laplace(loc=0.0, scale=(sen[0]/epsilon), size=(tf.shape(x)[0], img_size_cropped*img_size_cropped*num_channels))