Fan: Sun’iy intellekt va neyron to’rlar Guruh 21. 06 2022-2023- o’quv yili


Download 17.32 Kb.
Sana08.06.2023
Hajmi17.32 Kb.
#1465516
Bog'liq
Mo\'minov Inomjon 21.06


Fan: Sun’iy intellekt va neyron to’rlar Guruh 21.06 2022-2023- O’quv yili

1-topshiriq

import tensorflow as tf

# Kirish va chiqish ma'lumotlarini sozlang
x_data = [[0, 0], [0, 1], [1, 0], [1, 1]]
y_data = [[0], [1], [1], [0]]

# Kirish qatlamini ikkita neyronli va chiqish qatlamini bitta neyron bilan o'rnating


x = tf.placeholder(tf.float32, shape=[None, 2])
y_true = tf.placeholder(tf.float32, shape=[None, 1])
W = tf.Variable(tf.zeros([2, 1]))
b = tf.Variable(tf.zeros([1]))

# Faollashtirish funktsiyasini aniqlang (bu holda sigmasimon funktsiya)


y_pred = tf.sigmoid(tf.matmul(x, W) + b)

# Yo'qotish funktsiyasini aniqlang (bu holda ikkilik o'zaro faoliyat entropiya)


loss = tf.reduce_mean(-(y_true * tf.log(y_pred) + (1 - y_true) * tf.log(1 - y_pred)))

# Optimizatorni aniqlang (bu holda gradient tushishi)


optimizer = tf.train.GradientDescentOptimizer(learning_rate=0.5)
train_step = optimizer.minimize(loss)

# O'zgaruvchilarni ishga tushiring va seansni boshlang


init = tf.global_variables_initializer()
sess = tf.Session()
sess.run(init)

# Modelni o'rgating


for i in range(10000):
sess.run(train_step, feed_dict={x: x_data, y_true: y_data})

# Modelni yangi ma'lumotlarda sinab ko'ring


print(sess.run(y_pred, feed_dict={x: [[0.9, 0.8]]}))

2-topshiriq


import numpy as np

class Perceptron:
def init(self, num_inputs):
self.weights = np.zeros(num_inputs + 1)

def predict(self, inputs):
summation = np.dot(inputs, self.weights[1:]) + self.weights[0]
if summation > 0:
activation = 1
else:
activation = 0
return activation

def train(self, training_inputs, labels, num_epochs):
for epoch in range(num_epochs):
for inputs, label in zip(training_inputs, labels):
prediction = self.predict(inputs)
self.weights[1:] += (label - prediction) * inputs
self.weights[0] += (label - prediction)
training_inputs = np.array([[0, 0], [0, 1], [1, 0], [1, 1]])
labels = np.array([0, 0, 0, 1])
perceptron = Perceptron(2)
perceptron.train(training_inputs, labels, 10)
print(perceptron.predict(np.array([1, 1]))) # outputs 1

3-topshiriq

import numpy as np
import tensorflow as tf
# kiritish maʼlumotlari
X = [[0, 0], [0, 1], [1, 0], [1, 1]]
# output data
y = [[0], [1], [1], [0]]
# neyron tarmoq qatlamlarini aniqlang
input_layer = tf.keras.layers.Input(shape=(2,))
hidden_layer_1 = tf.keras.layers.Dense(4, activation='sigmoid')(input_layer)
hidden_layer_2 = tf.keras.layers.Dense(4, activation='sigmoid')(hidden_layer_1)
output_layer = tf.keras.layers.Dense(1, activation='sigmoid')(hidden_layer_2)
model = tf.keras.models.Model(inputs=input_layer, outputs=output_layer)
model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy'])
model.fit(X, y, epochs=10000)
test_data = [[0, 0], [0, 1], [1, 0], [1, 1]]
predictions = model.predict(test_data)
print(predictions)

Bajardi: Mo’minov Inomjon Qabul qildi: I.Tojimamatov

Download 17.32 Kb.

Do'stlaringiz bilan baham:




Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©fayllar.org 2024
ma'muriyatiga murojaat qiling