sns.pairplot(data, hue='target')  # Good to explore the dataset
plt.show()
X = data.drop(['target'], axis=1).values 
n_cols = X.shape[1] # Get the number of features other than target column
from tensorflow.keras.utils import to_categorical # For categorical target variable, you need something like one-hot-encoding
y = to_categorical(data['target']) # Use this for one-hot-encoding if target is a class and it is a classification problem
from tensorflow.keras.layers import Dense
from tensorflow.keras.models import Sequential
def create_model(optimizer='adam', activation='relu', nl=1,nn=256):
  model = Sequential()
  # for i in range(nl): # you can also automate this process of creating layers and neurons with specified arguments
  # # Layers have nn neurons
  #   model.add(Dense(nn, activation=activation))
  # Start with the first hidden layer, where information comes in shape (dataset column, any number of rows)
  model.add(Dense(100, activation=activation, input_shape = (n_cols,))) # First hidden layer, where values directly come from dataset of n features, with unknown number of datapoints
  model.add(Dense(100, activation=activation, kernel_initializer='normal')) # Second hidden layer
  model.add(Dense(100, activation=activation)) # Third hidden layer
  from tensorflow.keras.layers import BatchNormalization
  model.add(BatchNormalization()) # Add batch normalization for the outputs of the layer above
  # use 'softmax' for multi-class classification, 'sigmoid' for binary or multi-label classification
  model.add(Dense(3, activation='softmax')) # Output layer, 3 nodes for 3 class prediction
  my_optimizer = SGD(lr=lr) # # You can also use custom optimizer like this
  # For loss function, use 'categorical_crossentropy' for multiclass, 'binary_crossentropy' for binary or multi-label, 'mean_squared_error'  for regression
  model.compile(optimizer=optimizer, loss='categorical_crossentropy', metrics=['accuracy']) # optimizer = my_optimizer
  return model
# K-fold is costly for neural network, so use validation data as a wise choice to get best validation score with early stopping
from tensorflow.keras.callbacks import EarlyStopping
early_stopping_monitor = EarlyStopping(monitor='val_loss', patience=2) # Terminates once validation loss stops improving
# Instantiate a model checkpoint callback, this will automatically save the model when best result is produced
from keras.callbacks import ModelCheckpoint
model_save = ModelCheckpoint('best_model.hdf5', save_best_only=True)
model = create_model()
model_1_training = model.fit(X_train, y_train, validation_split=0.3, epochs=20, batch_size=128, callbacks = [early_stopping_monitor, model_save], verbose=0) # or use "validation_data=(X_test, y_test)" 
predictions = model.predict(X_test)
probability_true = predictions[:,1] 
from tensorflow.keras.models import load_model
init_weights = model.get_weights() # Save model weights
model.save('model_file.h5') # Saving  model
first_layer = model.layers[0] # Acessing the first layer of a Keras model
print(first_layer.input) # Printing the layer input (this is a tensorflow tensor object)
print(first_layer.output) # Printing the layer output  (this is a tensorflow tensor object)
print(first_layer.weights) # Printing the layer weights (this is a tensorflow variable object, which is updatable)
my_model = load_model('model_file.h5') # loading model
my_model.summary() # See model summary
history = model_1_training # Extracting fitting history
print(history.params) # See all parameters
print(history.history.keys()) # See what you can extract, eg :  val_loss = history.history['val_loss'], val_accuracy = history.history['val_accuracy']
accuracy = model.evaluate(X_test, y_test)[1] # Evaluate
preds = model.predict(test_set) # Predict 
preds_chosen = [np.argmax(pred) for pred in preds] # Extract the position of highest probability from each pred vector (For classification of multi-class problem)
### VISUALIZE KERAS LEARNING CURVE with plot_loss(loss,val_loss) and plot_accuracy(acc,val_acc) using fitting/training history
# k-fold Cross validation and Fine-tuning with RandomSearchCV
from tensorflow.keras.wrappers.scikit_learn import KerasClassifier # Import sklearn wrapper from keras
model = KerasClassifier(build_fn=create_model, epochs=6, batch_size=16) # Create a model as a sklearn estimator
from sklearn.model_selection import cross_val_score
kfold = cross_val_score(model, X, y, cv=5) # Check how your keras model performs with 5 fold crossvalidation
kfold.mean() # Print the mean accuracy per fold
params = dict(optimizer=['sgd', 'adam'], epochs=3, batch_size=[5, 10, 20], activation=['relu','tanh'], nl=[1, 2, 9], nn=[128,256,1000])
random_search = RandomizedSearchCV(model, params_dist=params, cv=3)
random_search_results = random_search.fit(X, y)
random_search_results.best_score_
random_search_results.best_params_

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

python parquet file to csv

python best practices

yolov5 without net

save variable as pkl python

python [-9:]

eigenface python

'DataFrame' object has no attribute 'dtype'

unable to enable maximize window tkinter

rabbit and fox numpy python

lstm in keras

neural network in keras

resnet50 in keras

autoencoder in keras

cnn in keras

tensor in keras

pyTelegramBotAPI edit photo

print api python

how to get values but not index from pandas series

how to get mode of a column from pandas

bayesian neural network pymcmc