-
Notifications
You must be signed in to change notification settings - Fork 35
/
Copy pathcnn_example.py
55 lines (40 loc) · 1.56 KB
/
cnn_example.py
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
import tensorflow as tf
import numpy as np
from sklearn.linear_model import LinearRegression
from sklearn.metrics import r2_score, mean_squared_error
INPUT_DIM = (np.random.randint(1, 33), np.random.randint(1, 33), 3)
NUM_LAYERS = np.random.randint(1, 4)
OUTPUT_DIM = np.random.randint(1, 129)
def random_model():
inp = tf.keras.Input(shape=INPUT_DIM)
for layer in range(NUM_LAYERS):
if layer == 0:
x = tf.keras.layers.Conv2D(
np.random.randint(1, 33),
(3, 3),
bias_initializer='glorot_uniform',
activation='linear'
)(inp)
else:
x = tf.keras.layers.Conv2D(
np.random.randint(1, 33),
(3, 3),
bias_initializer='glorot_uniform',
activation='linear'
)(x)
if np.random.uniform() > .5:
x = tf.keras.layers.AveragePooling2D()(x)
x = tf.keras.layers.Flatten()(x)
out = tf.keras.layers.Dense(OUTPUT_DIM, bias_initializer='glorot_uniform')(x)
return tf.keras.Model(inputs=inp, outputs=out)
if __name__ == '__main__':
model = random_model()
model.summary()
X = np.random.uniform(size=(8192, *INPUT_DIM))
y = model(X)
linear_model = LinearRegression().fit(X.reshape(8192, -1), y)
X_test = np.random.uniform(size=(1024, *INPUT_DIM))
y_true = model(X_test)
y_pred = linear_model.predict(X_test.reshape(1024, -1))
print(f'r2 score: {r2_score(y_true, y_pred)}')
print(f'mse: {mean_squared_error(y_true, y_pred)}')