2017-04-06 6 views
1

cifar10でプライバシオートエンコーダを構築しようとしていますが、Magnus Erik Hvass Pedersen(https://github.com/Hvass-Labs/TensorFlow-Tutorials/blob/master/06_CIFAR-10.ipynb)のcnnチュートリアルを参照しています。 これは私のコードです:損失関数のグローバル感度を計算するときcifar10 autoencoderでエラーが発生しました:dtype floatでプレースホルダテンソル 'x'の値を入力する必要があります

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"]()]] 

loss_restruction()関数にsession.run(tf.local_variables_initializer())を追加しようとしましたが、機能しませんでした。私は、lap_noise1がsen [0]tf.shape(0)[0]を呼び出してエラーが発生したと思います。この部分はどのように修正できますか?

答えて

0

問題は、このようにあなたは何のエラーメッセージの状態である、xに値を供給しなければならない、あなたの入力プレースホルダxによって異なりますglobal_sensitivityを要求している。このラインここ

sen=session.run(global_sensitivity) 

です。

私はあなたのコードを持ついくつかの問題を参照してください。あなたはまず自分のグラフを構築し、セッションを作成している

  • を。私はそれが一般的に有害であるかどうかは分かりません。通常は、まずグラフを作成し、それをセッションで起動します。
  • 最初にセッションを作成していて、グラフを作成するのはおそらくまだ機能していますが、そのエラーメッセージが表示されない場合でも、関数loss_restruction(images,decoder_layer_1, x_tilde)は意図したとおりに機能しません。ここでは、仲介結果をCPUにダウンロードし、その計算結果をグラフで再度使用して損失を計算します。ただし、loss_restructionという関数は、、つまりと1回だけ呼び出されます。つまり、グラフを作成するときです。したがって、sen = session.run(global_sensitivity)ステートメントはと1度だけ実行されます。も同様です。私。値senは実際にはトレーニング時に固定されており、最適化中は変更されません。したがって、私はあなたのモデルのトレーニングがうまくいくとは思っていません。なぜなら、バックプロパゲーションはあなたが期待するかもしれない勾配を計算しないからです。

sen = session.run(global_sensitivity)ステートメントを省略できるようにコードを書き直すことをお勧めします。あなたはそれのためにしなければならないすべてはTensorflowでこれらの3つのライン実装されています

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)) 

をラプラス分布はTensorflow、チェックhereに実装しているようです。

+0

テンソルフロー関数を使用してlap_noiseを生成しようとすると、私のロジックエラーでいくつかの問題が残っていますが、コードで構文エラーが解決されています。 : –

+0

論理エラーはどういう意味ですか? – kaufmanu

+0

それは損失がNaN%です。私は多分何かを間違って書くと思う。私はコードを再チェックします。ありがとうございました!もう一つの疑問は、テンソルフローAPIで見つけることのできる関数がないときです。この問題をどのようにして解決できますか? –

関連する問題