how to make a input tensor trainable

Viewed 1094

The following is the code trying to use the input image as a training variable during the optimization. It starts with a keras model and convert that to a tensorflow model. This tensorflow model take a tensor as input and try to optimize the cost function using the input tensor as a trainable variable.

The error is:

NotImplementedError: ('Trying to update a Tensor ', )

The reason is that the input tensor is not a variable. The question is how to make the input image trainable or cast a tensor to a tf.variable. Help is appreciated:

import tensorflow as tf
from keras.models import Sequential, load_model, Model
from keras import backend as K
from keras.layers.core import Dense, Dropout, Activation
import os
from tensorflow.python.framework import graph_util
from tensorflow.python.framework import graph_io

n_classes = 10

model = Sequential()
model.add(Dense(10, input_shape=(784,)))
model.add(Activation('relu'))                            
model.add(Dense(n_classes, name='logits'))
model.add(Activation('softmax'))
model.compile(loss='categorical_crossentropy', metrics=['accuracy'],  optimizer='adam')

# Write the graph in binary .pb file
outdir = "model4_tf"
try:
    os.mkdir(outdir )
except:
    pass


prefix = "simple_nn" 
name = 'output_graph.pb'
# Alias the outputs in the model - this sometimes makes them easier to access in TF
pred = []
pred_node_names = []
for i, o in enumerate(model.outputs):
    pred_node_names.append(prefix+'_'+str(i))
    pred.append(tf.identity(o, 
                            name=pred_node_names[i]))
print('Output nodes names are: ', pred_node_names)


sess = K.get_session()


constant_graph = graph_util.convert_variables_to_constants(sess,                                         
sess.graph.as_graph_def(), pred_node_names)
graph_io.write_graph(constant_graph, outdir, name, as_text=False)


tf.reset_default_graph()

def load_graph(model_name):
    #graph = tf.Graph()
    graph = tf.get_default_graph()
    graph_def = tf.GraphDef()
    with open(model_name, "rb") as f:
        graph_def.ParseFromString(f.read())
    with graph.as_default():
        tf.import_graph_def(graph_def)
    return graph

my_graph = load_graph(model_name=os.path.join(outdir, name))


# In[15]:

input_op = my_graph.get_operation_by_name("import/dense_1_input")
output_op = my_graph.get_operation_by_name("import/simple_nn_0")
logit_op = my_graph.get_operation_by_name("import/logits/BiasAdd")


x_hat = input_op.outputs[0] # input tensor
labels = output_op.outputs[0] # label tensor
logits = logit_op.outputs[0] # logits tensor

learning_rate = tf.placeholder(tf.float32, ())

loss = tf.nn.softmax_cross_entropy_with_logits_v2(logits=logits, labels=[labels])
optim_step = tf.train.GradientDescentOptimizer(learning_rate).minimize(loss,     var_list=[x_hat])
0 Answers
Related