Neural Networks Beginnings

- -
- 100%
- +
for j in range(num_items):
if ratings[i][j] > 0:
error = ratings[i][j] – np.dot(user_matrix[i,:], item_matrix[:,j])
user_matrix[i,:] += learning_rate * (error * item_matrix[:,j])
item_matrix[:,j] += learning_rate * (error * user_matrix[i,:])
#predict ratings for all users and items
predicted_ratings = np.dot(user_matrix, item_matrix)
#recommend items for a specific user
user_id = 0
recommended_items = np.argsort(predicted_ratings[user_id])[::-1]
print("Recommendations for user", user_id)
print(recommended_items)
In this example, we used matrix factorization to build a recommender system. We initialized user and item matrices with random values and trained them based on known user and item ratings. We then used the obtained matrices to predict ratings for all users and items, and then recommended items based on these predictions for a specific user. In real systems, more complex algorithms and more diverse data can be used.
4. Automatic emotion detection.
Process description.
We import the necessary modules from TensorFlow.
We create a model using convolutional neural networks. The model takes input data in the form of a 48x48x1 pixel image. Conv2D, BatchNormalization, and MaxPooling2D layers are used to extract features from the image. The Flatten layer converts the obtained features into a one-dimensional vector. Dense, BatchNormalization, and Dropout layers are used to classify emotions into 7 categories (happiness, sadness, anger, etc.). We compile the model, specifying the optimizer, loss function, and metrics. We train the model on the training dataset using the validation dataset.We evaluate the accuracy of the model on the testing dataset. We use the model to predict emotions on new data.
import tensorflow as tf
from tensorflow import keras
from tensorflow.keras import layers
# Creating a model
model = keras.Sequential([
layers.Conv2D(32, (3, 3), activation='relu', input_shape=(48, 48, 1)),
layers.BatchNormalization(),
layers.MaxPooling2D(pool_size=(2, 2)),
layers.Dropout(0.25),
layers.Conv2D(64, (3, 3), activation='relu'),
layers.BatchNormalization(),
layers.MaxPooling2D(pool_size=(2, 2)),
layers.Dropout(0.25),
Конец ознакомительного фрагмента.
Текст предоставлен ООО «Литрес».
Прочитайте эту книгу целиком, купив полную легальную версию на Литрес.
Безопасно оплатить книгу можно банковской картой Visa, MasterCard, Maestro, со счета мобильного телефона, с платежного терминала, в салоне МТС или Связной, через PayPal, WebMoney, Яндекс.Деньги, QIWI Кошелек, бонусными картами или другим удобным Вам способом.



