This program creates a simple dataset where the label is 1 if the sum of features is positive, else 0. It trains a small neural network to learn this pattern and prints predictions for the first 5 samples and the final accuracy.
import tensorflow as tf
import numpy as np
# Create dummy data: 100 samples, 3 features
np.random.seed(0)
X = np.random.randn(100, 3)
# Labels: 0 or 1 based on sum of features > 0
Y = (np.sum(X, axis=1) > 0).astype(int)
# Build model
model = tf.keras.Sequential([
tf.keras.layers.Dense(5, activation='relu', input_shape=(3,)),
tf.keras.layers.Dense(1, activation='sigmoid')
])
model.compile(optimizer='adam', loss='binary_crossentropy', metrics=['accuracy'])
# Train model
history = model.fit(X, Y, epochs=10, batch_size=10, verbose=0)
# Predict on first 5 samples
predictions = model.predict(X[:5])
# Print predictions rounded to 0 or 1
print('Predictions:', (predictions > 0.5).astype(int).flatten())
# Print final training accuracy
final_acc = history.history['accuracy'][-1]
print(f'Final training accuracy: {final_acc:.2f}')