from tensorflow.contrib.keras.python.keras import layers import numpy as np from tensorflow.contrib.keras.python.keras.models import Model from tensorflow.contrib.keras.python.keras.optimizers import Adam # load dataset: image arrays, labels arrays from prep_data_utils_01_save_load_large_arrays_bcolz_np_pickle_torch import bz_load_array val_img_array = bz_load_array("/Users/Natsume/Downloads/data_for_all/dogscats/results/val_img_array") val_lab_array = bz_load_array("/Users/Natsume/Downloads/data_for_all/dogscats/results/val_lab_array") # load dataset: image iterator and label iterator from prep_data_02_vgg16_catsdogs_03_img_folder_2_iterators import val_batches # create an constant tensor tensor1 = K.constant(value=val_img_array, name='tensor1') # create a input tensor (constant) input_tensor = layers.Input(tensor=tensor1, name='input_tensor') # create a input tensor (placeholder) without knowing num of samples input_tensor = layers.Input(shape=val_img_array.shape[1:], name='input_tensor') # create a relu tensor (placeholder) with input tensor relu_tensor = K.relu(input_tensor) """ def relu(x, alpha=0., max_value=None): Rectified linear unit With default values, it returns element-wise `max(x, 0)`
Goals: How to use the following 1. K.constant() 2. K.placeholder() 3. Input() """ from tensorflow.contrib.keras.python.keras.layers import Input from prep_data_utils_01_save_load_large_arrays_bcolz_np_pickle_torch import bz_load_array test_img_array = bz_load_array( "/Users/Natsume/Downloads/data_for_all/dogscats/results/test_img_array") from tensorflow.contrib.keras.python.keras import backend as K input = K.constant(test_img_array, name='test_img_tensor') """ Inputs: - value: scalar or array is a must - dtype, shape: optional, can be inferred from value or array - name are optional, but good to have one Return: - tensor def constant(value, dtype=None, shape=None, name=None): ('Creates a constant tensor.\n' '\n' 'Arguments:\n' ' value: A constant value (or list)\n'
from tensorflow.contrib.keras.python.keras.layers import Input, Dense from tensorflow.contrib.keras.python.keras import backend as K import numpy as np input_tensor = Input(shape=(100, ), name="input_tensor") inter_tensor = Dense(30, name="my_layer")(input_tensor) final_tensor = Dense(30, name="final_layer")(inter_tensor) model = Model(input_tensor, final_tensor) # create the original model layer_name = 'my_layer' intermediate_layer_model = Model(inputs=model.input, outputs=model.get_layer(layer_name).output) # must compile before predict? No, but must compile before training input_array1 = np.random.random((1000, 100)) * 9 input_tensor1 = K.constant(value=input_array1) intermediate_output = intermediate_layer_model.predict( input_array1) # return array intermediate_output1 = intermediate_layer_model( input_tensor1 ) # return tensor not array; tensor is no use and a long way to go to reach array """ Alternatively, you can build a Keras function that will return the output of a certain layer given a certain input, for example: """ from tensorflow.contrib.keras.python.keras import backend as K # with a Sequential model get_3rd_layer_output = K.function([model.layers[0].input], [model.layers[1].output]) layer_output = get_3rd_layer_output([input_array1
def __call__(self, shape, dtype=None): return K.constant(self.value, shape=shape, dtype=dtype)