# Very simple model with 1 input layer and 1 output layer
from tensorflow.keras.layers import Input, Dense, Concatenate, Add, Subtract, Multiply, Flatten, Embedding
input_tensor = Input(shape=(1,)) # One column
layer1 = Dense(1, name='Layer-1') # This layer is a transformation function that transforms input tensors into output tensors
layer1_output_tensor = layer1(input_tensor) # for 1 incoming input neuron information in output neuron, y= mx + b. There are 2 parameters, m and b 
from tensorflow.keras.models import Model
model = Model(input_tensor, layer1_output_tensor) # We are creating model using the flow of tensors in each sequence of layers
model.compile(optimizer='adam', loss='mae') # Compile model with loss function and optimizer
model.summary()
# VISUALIZE KERAS MODEL with plot_model

model.fit(X_train, y_train, batch_size=64, validation_split=.20, verbose=True) # Train
model.evaluate(X_test, y_test) # Test
### If there is any column with high cardinality categorical integer value, categorical embedding layer is used to map int to float
input_tensor = Input(shape=(1,)) # One column as (This input layer is just tensors and not an actual layers)
# input_dim=size of unique tokens, input_length= length of input sequence, output_dim= dense vector embedding matrix columns in float
layer1 = Embedding(input_dim=n_unique_categories, input_length=1, output_dim=1, name='Team-Strength-Lookup') # 1 value as input, 1 value as output
layer1_output_tensor = layer1(input_tensor)
# Embedding layers increase the dimensionality of data (The third dimension), so we need to flatten it with Flattening layer
layer2 = Flatten()
final_output_tensor = layer2(layer1_output_tensor)
# Creating a model with Model, which will also allow us to share the model with other layers
model = Model(input_tensor, final_output_tensor) # Construct the flow of tensors with input tensor and output tensor
# Sharing the model
input_tensor_1 = Input((1,))
input_tensor_2 = Input((1,))
output_tensor_1 = model(input_tensor_1) # # Takes in incoming tensors, the whole model acts as a single layer
output_tensor_2 = model(input_tensor_2) # # Takes in incoming tensors, the whole model acts as a single layer
### Sharing single layer
input_tensor_1 = Input((1,))
input_tensor_2 = Input((1,))
shared_layer = Dense(1)
output_tensor_1 = shared_layer(input_tensor_1) # Takes in incoming tensors
output_tensor_2 = shared_layer(input_tensor_2) # Takes in incoming tensors
### Create model with Merging layers
in_tensor_1 = Input((1,))
in_tensor_2 = Input((1,))
merge_layer = Concatenate() # There are also Add, Subtract, Multiply merging layers
out_tensor = merge_layer([in_tensor_1, in_tensor_2]) # Takes in a list of incoming tensors
model = Model([in_tensor_1, in_tensor_2], out_tensor)
model.compile(optimizer='adam', loss='mean_absolute_error')
model.fit([data_1, data_2], target) # A list of series as X, and the target series as y which maps to : model.fit(X,y)
in_tensor_1 = Input(shape=(1,))
in_tensor_2 = Input(shape=(1,))
in_tensor_3 = Input(shape=(1,))
out_tensor = Concatenate()([in_tensor_1, in_tensor_2, in_tensor_3])
output_tensor = Dense(1)(out_tensor)
### Stacking models : Requires 2 datasets. Train model1 using dataset1. then predict dataset2 outcome with model1. Store the model1_prediction in dataset2. Now create model2 with model1_prediction column and other numeric variables to get the final output
pred = model1.predict([in_data_1, in_data_2, in_data_3]) # These are 3 series of X
df['pred'] = pred
df[['home','seed_diff','pred']]
# Fitting model with 2 outputs
X = df[['seed_diff']]
y = df[['score_1', 'score_2']] # 2 columns for 2 outputs
model.fit(X, y, epochs=500)
model.get_weights() # See model weights and biases
# A regressor classifier : Placing a classification layer with sigmoid activation function on top of a regression layer (data first go to regression layer, which then converts to a class after going through classification layer with the help of sigmoid function)
# Best use case  : penalize for increased chance instead of directly 
input_tensor = Input(shape=(1,))
regression_layer = Dense(1)
regression_tensor = regression_layer(input_tensor)
classification_layer = Dense(1, activation='sigmoid')
classification_tensor = classification_layer(regression_tensor)
model = Model(input_tensor, [regression_tensor, classification_tensor]) # since there are 2 decision layers we want to extract information from, they should be in a list
model.compile(loss=['mean_absolute_error', 'binary_crossentropy'], optimizer='adam') # 2 loss function for 2 decision layers
X = df[['abc_feature']]
y_reg = df[['reg_col']]
y_class = df[['class_col']]
model.fit(X, [y_reg, y_class], epochs=100)
# Manually view the probability using weight and bias
model.get_weights() # Get weight and bias for each layer from here, should be in numpy format [[weight]] , [bias]
from scipy.special import expit as sigmoid
print(sigmoid(1 * weight + bias))

Python相关代码片段

creat key value jetstream python api

lambda python tkinter

add data into json python

create admin in Django

Convert a string to an int in Python

python named parameters command line

Return a list from a function in Python

keras decoder of the inference model

teacher forcing in keras

df.rename(columns= 'a' 'b' )

Python check whether object is picklable

tkinter custom icons

title of a webpage using selenium python

what does DataFrame.factorize do in pandas

tensorflow mse

encoder decoder architecture in keras

pathlib replace extension

pandas access datetime filed

python stopwatch decorator

There is more than one Python

tkinter combobox get value

beautifulsoup returns empty list

importerror: numba needs numpy 1.21 or less

how to install jdk in windows 10

combo box tkinte

run python script on raspberry pi command line

minus infinity in python

hallar angulo de referencia python

create filtered pivot tables in pandas

text in keras

how to mirror screen on tv with python

python read file to string

python import tuple

python sefine function type

meta llama 3 base huggingface

equal to or more than python

equal to or less than python

python pillow transparent background

CLEAR ALL MIGRATIONS DJANGO

decrement in python

hugging face to dataframe

Table Creation and Data Insertion in PySpark

Testing ETL Framework for Fixed-Length Files

Making Bulk SQL Queries from Notebooks

dictionary of tuple python

kml to csv with scraping

kml to csv without scraping

gan in keras

Qrcode Python

No module named 'GeminiAIChat'

How not to write Python codes

tensorflow basics

GET in python

deep learning in tensorflow

custom tensorflow model

pathlib python

pandas excelwriter

python practise

key,value en python

linear regression in tensorflow

spark dataframe get column

drop one table sqlalchemy

python code to remove last character from string

fastapi get body on http middleware

custom neural network in keras

python selenium execute_script

db model for blog

yolov5 opencv

Get first 100 lines of file - python

python playground

print number pattern using for loop in python

ollama python

no module named 'wget'

No module named 'langchain'

failed to build wxpython

python vs c#

rabbitmq python example

change the django url prefix name

np.linspace is not defined python

LLM beguiner guide python