Ejemplo n.º 1
0
    def test_defuzzy3(self):
        odim = 3
        input_dim = 2
        batch = 2
        sess = K.get_session()

        layer = DefuzzyLayer(odim)
        layer.build(input_shape=(batch, input_dim))

        x = K.placeholder(shape=(batch, input_dim))
        rules_outcome = K.placeholder(shape=(input_dim, odim))
        layer.rules_outcome = rules_outcome
        xc = layer.call(x)

        xx = [[0.2, 0.3], [0.3, 0.2]]
        cc = [[1, 2, 3], [0, 1, 0]]
        vals = sess.run(xc, feed_dict={x: xx, rules_outcome: cc})
        self.assertEqual(len(vals), batch)
        self.assertEqual(len(vals[0]), odim)
        self.assertAlmostEqual(vals[0][0], 0.2, 7)
        self.assertAlmostEqual(vals[0][1], 0.7, 7)
        self.assertAlmostEqual(vals[0][2], 0.6, 7)
        self.assertAlmostEqual(vals[1][0], 0.3, 7)
        self.assertAlmostEqual(vals[1][1], 0.8, 7)
        self.assertAlmostEqual(vals[1][2], 0.9, 7)
Ejemplo n.º 2
0
for i in vals:
    x.append([r * m.cos(i), r * m.sin(i)])
    y.append(i)

for i in np.linspace(0, 1.9 * m.pi, num=25):
    x_test.append([r * m.cos(i), r * m.sin(i)])
    y_test.append(i)

x_train = np.array(x)
y_train = np.array(y)

f_layer = FuzzyLayer(20, input_dim=2)
model = Sequential()
model.add(f_layer)
#model.add(Dense(20, activation='sigmoid'))
model.add(DefuzzyLayer(1))

model.compile(loss='logcosh', optimizer='rmsprop', metrics=['mae'])

model.fit(x_train, y_train, epochs=500, verbose=0, batch_size=100)

y_pred = model.predict(np.array(x_test))

weights = f_layer.get_weights()
print(weights)

plt.ion()
plt.show()
plt.clf()
plt.title('circle')
plt.ylabel('V')
Ejemplo n.º 3
0
    ([1, 0, 0, 0] if a[0] < 0 and a[1] < 0 else [0, 1, 0, 0] if a[0] < 0
     and a[1] > 0 else [0, 0, 1, 0] if a[0] > 0 and a[1] < 0 else [0, 0, 0, 1])
    for a in x_train
])

model = Sequential()

model.add(
    FuzzyLayer(
        16,
        input_dim=2,
        initial_centers=[[15, 0, 15, 0, 1, 1, 1, 1, 15, 0, 15, 0, 1, 1, 1, 1],
                         [0, 15, 15, 0, 1, 1, 1, 1, 15, 0, 15, 0, 1, 1, 1, 1]],
        initial_sigmas=[[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
                        [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]]))
model.add(DefuzzyLayer(4))
#model.add(Dense(4, activation='sigmoid'))

model.compile(loss='logcosh', optimizer='rmsprop', metrics=['mae', 'acc'])

model.fit(x_train, y_train, epochs=100, verbose=0, batch_size=10)

# %%
assert np.argmax(model.predict(np.array([[1, 1]]))) == 3
# %%
assert np.argmax(model.predict(np.array([[-1, -1]]))) == 0
# %%
assert np.argmax(model.predict(np.array([[-1, 1]]))) == 1
# %%
assert np.argmax(model.predict(np.array([[1, -1]]))) == 2
# %%