<jupyter_start><jupyter_text><jupyter_code>import tensorflow as tf<jupyter_output><empty_output><jupyter_text>a Toy Resnet Model<jupyter_code>inputs = tf.keras.Input((32, 32, 3))
x = tf.keras.layers.Conv2D(32, 3, activation="relu")(inputs)
x = tf.keras.layers.Conv2D(64, 3, activation="relu")(x)
block_1_output = tf.keras.layers.MaxPooling2D(3)(x)

x = tf.keras.layers.Conv2D(64, 3, activation="relu", padding="same")(block_1_output)
x = tf.keras.layers.Conv2D(64, 3, activation="relu", padding="same")(x)
block_2_output = tf.keras.layers.add([x, block_1_output])

x = tf.keras.layers.Conv2D(64, 3, activation="relu", padding="same")(block_2_output)
x = tf.keras.layers.Conv2D(64, 3, activation="relu", padding="same")(x)
block_3_output = tf.keras.layers.add([x, block_2_output])

x = tf.keras.layers.Conv2D(64, 3, activation="relu")(block_3_output)
x = tf.keras.layers.GlobalAveragePooling2D()(x)
x = tf.keras.layers.Dense(256, activation="relu")(x)
x = tf.keras.layers.Dropout(0.5)(x)
outputs = tf.keras.layers.Dense(10)(x)

model = tf.keras.Model(inputs, outputs)
tf.keras.utils.plot_model(model, show_shapes=True)
(xtrn, ytrn), (xtst, ytst) = tf.keras.datasets.cifar10.load_data()
xtrn = xtrn.astype("float64") / 255.0
xtst = xtst.astype("float64") / 255.0

ytrn = tf.keras.utils.to_categorical(ytrn, 10)
ytst = tf.keras.utils.to_categorical(xtrn, 10)
model.compile(loss=tf.keras.losses.CategoricalCrossentropy(from_logits=True),
              optimizer=tf.keras.optimizers.RMSprop(1e-3),
              metrics=["accuracy"])
xtrn[0].shape
model.fit(xtrn, ytrn, batch_size=256, epochs=1, validation_split=0.2)
z = model.predict(xtrn[:1, :])
dir(model)
ytrn[0]
z.argmax()
dir(z)<jupyter_output><empty_output><jupyter_text>Shared Layers
 shared layers are layer instances that are used mutiple times in model
 shared layers are often used to encode inputs from similar spaces(say two different pieace of text that feature similar vocabulary. This allows to sharing of information across different inputs and they make it possible train such model on less data<jupyter_code>emb = tf.keras.layers.Embedding(1000, 128)
i1 = tf.keras.Input((None, ), dtype="int32")
i2 = tf.keras.Input((None, ), dtype="int32")
o1 = emb(i1)
o2 = emb(i2)<jupyter_output><empty_output><jupyter_text>Extract and reuse nodes of graph of layers<jupyter_code>vgg19 = tf.keras.applications.VGG19()
feature_list = [l.output for l in vgg19.layers]
feature_extraction_model = tf.keras.Model(vgg19.inputs, feature_list)
feature_list
import numpy as np
img = np.random.random((1, 224, 224, 3)).astype("float32")
feature_extraction_model(img)<jupyter_output><empty_output><jupyter_text>Extend Api using custom layer<jupyter_code>class CustomLayer(tf.keras.layers.Layer):
  def __init__(self, units):
    super(CustomLayer, self).__init__()
    self.units = units
  
  def build(self, input_shape):
    self.w = self.add_weight(
        
        shape=(input_shape[-1], self.units),
        initializer="random_normal",
        trainable=True
    )
    self.b = self.add_weight(
        shape=(self.units,),
        initializer="random_normal",
        trainable=True
    )
  
  def call(self, inputs):
    return tf.matmul(inputs, self.w) + self.b

  def get_config():
    return {"Units": self.units}
ie = tf.keras.Input((4))
oe = CustomLayer(8)(ie)
model = tf.keras.Model(ie, oe)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.14656177156177155, 'fraction_numerical': 0.034382284382284384, 'mean_word_length': 4.808798646362098, 'pattern_counts': {'":': 1, '<': 21, '<?xml version=': 0, '>': 21, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '5', 'dir': 'jupyter-structured-clean-dedup', 'id': '317280', 'license': 'MIT', 'n_tokens_mistral': 1378, 'n_tokens_neox': 1310, 'n_words': 234, 'path': 'Tensorflow/TensorflowPrac13_KerasFunctionalApi_ToyResnet.ipynb', 'repo_name': 'Vinaypatil-Ev/vinEvPy-GoCoLab', 'size': '3432'}