# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python 3
#     name: python3
# ---

# + [markdown] id="view-in-github" colab_type="text"
# <a href="https://colab.research.google.com/github/AmanPriyanshu/Weird-Keras-Models/blob/master/weird_keras_models1.ipynb" target="_parent"><img src="https://colab.research.google.com/assets/colab-badge.svg" alt="Open In Colab"/></a>

# + [markdown] id="e-WNO-QGoW7N" colab_type="text"
# ## IMPORTS:

# + id="HeKzJutsoSb-" colab_type="code" colab={}
import tensorflow as tf
import numpy as np
import pandas as pd
import random
from matplotlib import pyplot as plt
from tensorflow.keras.models import Model
from tensorflow.keras.layers import Dense, Conv2D, Input, BatchNormalization, Add
from tensorflow.keras.layers import Flatten, GlobalAveragePooling2D, Concatenate, Dropout


# + [markdown] id="wTaoXJRmovTn" colab_type="text"
# ## BASE MODEL:

# + id="xvwBa6twocv3" colab_type="code" colab={}
def convBlock(x):
  conv = Conv2D(128, (3,3), strides=1, padding="same", activation='relu')(x)
  bn = BatchNormalization()(conv)
  return bn

def ResBlock(x):
  conv1 = Conv2D(128, kernel_size=(3,3), strides=1, padding="same", activation='relu')(x)
  bn1 = BatchNormalization()(conv1)
  conv2 = Conv2D(128, kernel_size=(3,3), strides=1, padding="same", activation='relu')(bn1)
  bn2 = BatchNormalization()(conv2)
  out = Add()([bn2, x]) 
  return out

def OutBlock(x):
  conv = Conv2D(3, kernel_size=(1,1), strides=1, activation='relu')(x)
  bn = BatchNormalization()(conv)
  flat = Flatten()(bn)
  dense1 = Dense((32), activation='relu')(flat)
  value = Dense(1, activation='tanh')(dense1) #Value Head
  
  conv = Conv2D(32, kernel_size=(1,1), strides=1, activation='relu')(x)
  bn = BatchNormalization()(conv)
  flat = Flatten()(bn)
  linear = Dense(6*7*32, activation='relu')(flat)
  policy = Dense(7, activation='softmax')(linear)

  return policy, value


# + id="it0N-XutvTan" colab_type="code" colab={}
input0 = Input([6, 7, 3])
model = convBlock(input0)
for _ in range(19):
  model = ResBlock(model)
policy, value = OutBlock(model)

model = Model(inputs=input0, outputs=[policy, value])

# + id="4Vsb3YIX5HZM" colab_type="code" colab={"base_uri": "https://localhost:8080/", "height": 1000} outputId="6be15c15-fc76-4d82-8a6e-bda4f4e3ea7a"
model.summary()

# + [markdown] id="lrs1Bkhw8G3D" colab_type="text"
# ## Custom Loss Function

# + id="5qDIn7ye93_Y" colab_type="code" colab={}
losses = {
	"category_output": "categorical_crossentropy",
	"color_output": "categorical_crossentropy",
}
lossWeights = {"category_output": 1.0, "color_output": 1.0}

# + id="POWX6HNU933j" colab_type="code" colab={}
model.compile(optimizer='adam', loss=losses, loss_weights=lossWeights)

StarcoderdataJupyterScriptsDedupFiltered