[파이썬] TensorFlow에서 TensorFlow Playground

tensorflow-logo

TensorFlow Playground is a web-based interactive tool for exploring and understanding neural networks. It allows users to experiment and visualize different network architectures and see how they affect the network’s accuracy and behavior. While TensorFlow Playground is a great tool for beginners and non-technical users, it has its limitations, especially when it comes to customizing the network architecture.

In this blog post, we will explore how to recreate TensorFlow Playground using TensorFlow and Python, giving us more flexibility and control over the network design.

Installing TensorFlow

Before we begin, make sure you have TensorFlow installed on your machine. You can install it using pip:

pip install tensorflow

Creating the Network

Let’s start by importing TensorFlow and creating a simple neural network with two hidden layers. We will use the Sequential model from TensorFlow’s keras API to build the network.

import tensorflow as tf
from tensorflow import keras

# Define the network architecture
model = keras.Sequential([
    keras.layers.Dense(8, activation='relu', input_shape=(2,)),
    keras.layers.Dense(16, activation='relu'),
    keras.layers.Dense(1, activation='sigmoid')
])

In the above code, we defined a network with two hidden layers consisting of 8 and 16 neurons, respectively. The input_shape=(2,) specifies that our input data has two features.

Compiling the Model

After creating the network architecture, we need to compile the model by specifying the loss function, optimizer, and evaluation metric. For simplicity, we will use binary cross-entropy as the loss function and Adam optimizer.

# Compile the model
model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy'])

Training the Model

Now that our model is compiled, we can train it on our dataset. For demonstration purposes, we will use a simple synthetic dataset where the output depends on the input’s position.

import numpy as np

# Generate synthetic dataset
X = np.random.rand(100, 2) * 2 - 1
y = np.array([1 if x[0] > 0.5 and x[1] > 0.5 else 0 for x in X])

# Train the model
model.fit(X, y, epochs=10)

In the above code, we generate a random 2-dimensional input and label the samples based on their position. We then train the model for 10 epochs using the fit method.

Evaluating the Model

After training the model, we can evaluate its performance on a test set. Again, we generate a synthetic test dataset and use the trained model to predict the outputs.

# Generate test dataset
X_test = np.random.rand(20, 2) * 2 - 1
y_test = np.array([1 if x[0] > 0.5 and x[1] > 0.5 else 0 for x in X_test])

# Evaluate the model
loss, accuracy = model.evaluate(X_test, y_test)
print(f'Test loss: {loss:.3f}, Test accuracy: {accuracy:.3f}')

Summary

In this blog post, we learned how to recreate TensorFlow Playground using TensorFlow and Python. We built a simple neural network, trained it on a synthetic dataset, and evaluated its performance.

By using TensorFlow and Python, we have the flexibility to experiment with different network architectures, loss functions, and optimizers. This allows us to gain a deeper understanding of neural networks and their behavior.

While TensorFlow Playground provides a user-friendly interface for exploring neural networks, the code examples shown here demonstrate how to create and customize networks programmatically, enabling more advanced experimentation.