-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_tanh.py
executable file
·44 lines (39 loc) · 1.2 KB
/
test_tanh.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
#!/usr/bin/env python3
import pandas
import numpy as np
import tensorflow as tf
# read data
dfnm = 'iusdata_raw.csv'
lblc = 'LABEL_NEC'
dtac = ['DATA_SBR', 'DATA_SBL',
'DATA_COLONR', 'DATA_COLONL',
'DATA_LUMENR', 'DATA_LUMENL']
# read initial data frame
dataframe = pandas.read_csv(dfnm)
ynp = dataframe[lblc].to_numpy(dtype=np.int32)
xnp = dataframe[dtac].to_numpy(dtype=np.float32)
print(xnp.shape, ynp.shape)
# tensorflow dataset package
batchsize = 8
data = tf.data.Dataset.from_tensor_slices((xnp,ynp))
data = data.shuffle(buffer_size=ynp.shape[0],
reshuffle_each_iteration=True).batch(batchsize)
print(data)
# build model
model = tf.keras.Sequential()
model.add(tf.keras.layers.Dense(units=1,
activation='tanh',
input_shape=[xnp.shape[-1]]))
model.summary()
# compile
model.compile(optimizer=tf.keras.optimizers.SGD(),
loss=tf.keras.losses.MeanSquaredError(),
metrics=['accuracy'])
# fit
tot = ynp.shape[0]
pos = np.bincount(ynp)[1]
neg = tot - pos
w0 = (1.0 / neg) * (tot / 2.0)
w1 = (1.0 / pos) * (tot / 2.0)
cwts = {0:w0, 1:w1}
model.fit(data, epochs=5000, class_weight=cwts)