build environment with rye

here is my pyproject.toml:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
[project]
name = "tensorflow"
version = "0.1.0"
description = "Add your description here"
authors = [{ name = "Invoker-pray", email = "jiaohongbao04@gmail.com" }]
dependencies = [
"pip>=25.1.1",
"jupyter>=1.1.1",
"shap>=0.47.2",
"matplotlib>=3.10.3",
"torch>=2.7.1",
"torchvision>=0.22.1",
"torchaudio>=2.7.1",
"tf-nightly>=2.20.0.dev20250604",
"pandas>=2.3.0",
"pydot>=4.0.0",
]
readme = "README.md"
requires-python = ">=3.8, <=3.11"

[build-system]
requires = ["hatchling"]
build-backend = "hatchling.build"

[tool.rye]
managed = true
dev-dependencies = []

[tool.hatch.metadata]
allow-direct-references = true

[tool.hatch.build.targets.wheel]
packages = ["src/tensorflow"]

you can use this file, and run:

rye sync

to create the environment, actually, it will create a .venv file, just as the python-venv.

define a model

to check how to create a model

1
help(tf.keras.Model)

to see the parameters and model structure

1
model.summary()

to see it graphically

1
tf.keras.utils.plot_model(model, to_file='path of picture', show_shapes=True, show_layer_names=True)

groups a linear stack of layers into a model

1
help(tf.keras.Sequential)

how to use multi-GPU

1
help(tf.distribute.Strategy)

and we can write:

1
2
3
strategy = tf.distribute.MirroredStrategy()
# auto detect and use multi-GPU

after create a model, we should run:

1
2
3
4
5

model.compile()
model.fit()
model.valuate()
model.predict()

typically.

compile model

in keras, model must be compiled before fit() predict() and evaluate()

compile() is to tell tensorflow how to train the model, the most important thing is to specify the loss function(which deviation we want to minimize), optimizer(how to update weights) and metrics(which point we should pay attention to).

1
2
3
4
5
6
7
8
9
10
11
model.compile(
optimizer = 'adam',
loss = 'categorical_crossentropy',
metrics = ['accuracy'],
loss_weights = ,
weighted_metrics = ,
)

#more infos in
help(tf.keras.Model.compile)

train a model

we can use fit() to train the model.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
model.fit(
x = , # input train-dataset
y = , # output train_dataset
batch_size = , # amount of input data in a single batch
epochs = , # traveral all train dataset in a epoch
verbose = , # 0 for not output anythings; 1 for progress bar model; 2 for each epoch a line model.
callbacks = , # some functions that auto invoked while train, such as monitor, save, modify train process.

)


help(tf.keras.Model.fit) # for more infos



train model on a batch

1
2
3
4
5
6
7
8
9
loss_and_metrics = model.train_on_batch(x,y)

'''
x: input data in a single batch
y: label/output
sample_weight:(optional)

return: a list contains loss and other metircs
'''

create checkpoint

we can create checkpoint mannually:

1
2
3
4
5
6
model = tf.keras.Sequential()
optimizer = tf.keras.optimizer.Adam()

checkpoint = tf.train.Checkpoint(model=model, optimizer=optimizer)
checkpoint.save('path for save checkpoint')

or we can use callbacks:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
model = tf.keras.Sequential([
#layers info.
])
model.compile(optimizer='adam', loss='mse')

checkpoint_cb = tf.keras.callbacks.ModelCheckpoint(
filepath='model_checkpoints/ckpt-{epoch}.h5',
save_weights_only=True, # only save weight
save_best_only=True, # only save the best weight on train dataset(it means only update when a better result's emergence)
monitor='val_loss', # use val_loss as the metrics.
mode='min' # min is better.
)

model.fit(x, y, epochs = , validation_data=(x, y), callbacks = [checkpoint_cb])

resume from a checkpoint

1
2
3
checkpoint.restore(tf.train.latest_checkpoint('path to checkpoint'))

model.load_weights('model_checkpoints/ckpt-5.h5')

evaluate a model

1
2
3
4
5
6
7
8
9
10
11

model.evaluate(
x = ,
y = ,
batch_size = ,
steps = , # it will run for steps batchs while evaluate.

)


help(tf.keras.Model.evaluate) # for more infos.

fit and evaluate has _generator model, generator can return a batch of data in each yield, it may be use when dataset is too large.

predict from a model

1
2
3
4
5
6
7
8
9
10
11

model.predict(
x = ,
y = ,
batch_size = ,
steps = ,

)


help(tf.keras.Model.predict) # for more infos.

also has:

.predict_on_batch(), .predict_generator(), .predict_step().

save a model

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# keras save model.
model.save("my_model")
model.save("my_model.h5")
model = tf.keras.models.load_model("my_model")
model = tf.keras.models.load_model("my_model.h5")

# keras save weight.
model.save_weights("my_model_weights.ckpt")
model.load_weights("my_model_weights.ckpt")


# without keras
tf.save_model.save(model, "my_model")
loaded_model = tf.saved_model.load("my_model")

# save checkpoint without keras.


## create checkpoint target.
ckpt = tf.train.Checkpoint(step = tf.Variable(1), optimizer = optimizer, net = model)

ckpt.save("path to checkpoint")
ckpt.restore(tf.train.latest_checkpoint("path to checkpoint"))


other functions we may use

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# get parameters and layers infos.
model.summary()

tf.keras.utils.plot_model(model, to_file='path of picture', show_shapes=True, show_layer_names=True)


# add layers at the end of model
model.add(layer)

# use name/index to get the layer, ang we can get more infos and modify the layer's property

layer = model.get_layer( name/index )

# save and load model
model.save()
tf.keras.models.load_model()

# dataset of keras
keras.datasets # we can use keras.dataset quick load datasets(official datasets)
keras.datasets.cifar10.load_data()

# from logits get sample.
tf.random.categorical()

# load checkpoint to continue train if the last train is interrupted.
tf.train.load_checkpoint()

fundamental algorithm

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16

# linear regression

linear_regression = tf.estimator.LinearClassifier(feature_columns)

linear_regression.train(train_function)
linear_regression.evaluate(eval_function)
linear_regression.predict(test_function)


# DNN classification

c = tf.estimator.DNNClassifier(feature_columns, \, hidden_units, n_classes)
c.train(train_function, steps)


hyperparameters and built-in functions

built-in contents
tf.keras.optimizers SGD
Adam
Adagrad
RMSProp
tf.keras.losses BinaryCrossentropy
categorical_crossentropy
MeanAbsoluteError
MeanSquaredError
tf.keras.metrics Accuracy
AUC
False Positive
Precision

where is data?

operation for instance
create dataset from numpy from_tensor_slices((x,y))
create dataset from TFRecord TFRecordDataset(file_path)
pre-process .map(lambda x,y: (x/255.0, y))
shuffle .shuffle(buffer_size=100)
batch .batch(32)
repeat .repeat()
prefetch .prefetch(tf.data.AUTOTUNE)

what is tf.feature_column?

it can transform data in different format to tensor that nerual network can process.

assume we have data like this:

age gender income country
25 male 5000 US
32 female 7000 UK
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# we can extract numeric data.
age = tf.feature_column_numeric_column("age")
income = tf.feature_column_numeric_column("income")

# extract categorical features.
gender = tf.feature_column.categorical_column_with_vocabulary_list(kay="gender", vocabulary_list=["male", "female"])

# embedding feature.
gender_emb = tf.feature_column.embedding_column(gender, dimension=2)

# concatenate them.
feature_columns = [age, income, gender_emb, country_emb]

# create input layer.
input_layer = tf.keras.layers.DenseFeatures(feature_columns)

model = tf.keras.Sequential([
input_layer,
tf.keras.layers.Dense(64, activation='relu'),
tf.keras.layers.Dense(1)
])

what is inside of keras.layers?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121

### convolutional layers.
# Conv 1D is usually used to process audio data, Conv 2D for graphic data, Conv 3D for video that have one frame in each a timescale.
layers.Conv1D(
filters, # amount of cores.
kernel_size,
strides,
padding = , # 'same' or 'valid'
activation =,
use_bias = True,
kernel_initializer = 'globrot_uniform', # the apporch of initialize weights.
.........
)
layers.Conv2D()
layers.Conv3D()

# transpose convolutional layer, Deconvolution pr up-convolution, it can recover low resolution pics to high resolution pics.
keras.layers.Conv1DTranspose()

keras.layers.ZeroPadding1D() # zero padding.
keras.layers.Cropping1D()
keras.layers.UpSampling1D() # up sample.








### pooling layers.
#max pooling
keras.layers.MaxPool1D()
keras.layers.MaxPool2D()
keras.layers.MaxPool3D()

# average pooling
keras.layers.AveragePooling1D()

# global average and global maxpooling
keras.layers.GlobalAveragePooling1D()
keras.layers.GlobalMaxPool2D()










### activation layers.
keras.layers.Activation('relu')

# different types of ReLU:
keras.layers.ReLU()
keras.layers.LeakyReLU()
keras.layers.PReLU()








### dropout layers.
x = keras.layers.Dropout(rate=0.2)(x) # deactivate randomly.

model.add(SpatialDropout1D(0.2))










### embedding layers.
# map the discrete data/scatter to dense vectors.
keras.layers.Embedding()








### RNN / recurrent layers. for time/sequential valuable task.
keras.layers.LSTM()
keras.layers.RNN()
keras.layers.GRU()










# flatten layers. convert multi-dimension tensor to 1D vector, is used to connect Conv and Dense.
keras.layers.Flatten()










### dense layers.
keras.layers.Dense(32, activation='relu')

we need callbacks

we can use callbacks to see what happen inside the model.

keras.callbacks.ModelCheckpoint(): save keras model or weights with some frequency.

keras.callbacks.EarlyStopping(): when xxx happen, stop train.

data should be pre-process

pre-process data type description
image keras.preprocessing.iamge.load_img() # load image as arrays
keras.preprocessing.image.img_to_array()
keras.preprocessing.image.array_to_img()
keras.preprocessing.image.ImageGenerator # generate tensor image data in real time
text keras.preprocessing.text.Tokenizer()
keras.preprocessing.text.one_hot()
sequence keras.preprocessing.sequence.pad_sequences # padding to specified longth
keras.preprocessing.sequence.skipgrams() # generate skipgram pair

use pre-trained model

when the data is very similar to original dataset, we mat use the model we have had will save more time.

we can use keras.applications to use model that provide by tensorflow.

tensorflow.keras.applications.MobileNetV2()