{"id":2352,"date":"2026-07-24T00:33:18","date_gmt":"2026-07-23T17:33:18","guid":{"rendered":"https:\/\/sumberlaba.com\/index.php\/2026\/07\/24\/how-to-build-a-simple-neural-network-with-keras-a-step-by-step-tutorial\/"},"modified":"2026-07-24T00:33:18","modified_gmt":"2026-07-23T17:33:18","slug":"how-to-build-a-simple-neural-network-with-keras-a-step-by-step-tutorial","status":"publish","type":"post","link":"https:\/\/sumberlaba.com\/index.php\/2026\/07\/24\/how-to-build-a-simple-neural-network-with-keras-a-step-by-step-tutorial\/","title":{"rendered":"How to Build a Simple Neural Network with Keras: A Step-by-Step Tutorial"},"content":{"rendered":"<h1>How to Build a Simple Neural Network with Keras: A Step-by-Step Tutorial<\/h1>\n<p>Keras, now part of TensorFlow, is one of the easiest deep learning frameworks to get started with. In this tutorial, you&#8217;ll build a feedforward neural network for classifying handwritten digits from the MNIST dataset. You only need basic Python knowledge and a few lines of code.<\/p>\n<p>First, install TensorFlow (which includes Keras) using pip: <code>pip install tensorflow<\/code>. Then import the required modules and load the MNIST data.<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/sumberlaba.com\/wp-content\/uploads\/2026\/07\/article-1784827996164.jpg\" alt=\"Article illustration\" style=\"display:block;margin:20px auto;max-width:100%;height:auto;border-radius:8px;\" \/><\/p>\n<h2>1. Load and Preprocess the Data<\/h2>\n<p>Keras provides built-in datasets. Use <code>keras.datasets.mnist.load_data()<\/code> to get training and test images (28&#215;28 pixels). Normalize pixel values to the range [0,1] by dividing by 255.0. Then flatten the 2D images into 1D vectors of 784 features.<\/p>\n<pre><code>from tensorflow import keras\n(x_train, y_train), (x_test, y_test) = keras.datasets.mnist.load_data()\nx_train = x_train.reshape(-1, 784).astype('float32') \/ 255.0\nx_test = x_test.reshape(-1, 784).astype('float32') \/ 255.0\n<\/code><\/pre>\n<h2>2. Build the Model with the Sequential API<\/h2>\n<p>Create a sequential model by stacking layers. Use a hidden layer with 128 neurons and ReLU activation, then an output layer with 10 neurons (one per digit) and softmax activation.<\/p>\n<pre><code>model = keras.Sequential([\n    keras.layers.Dense(128, activation='relu', input_shape=(784,)),\n    keras.layers.Dense(10, activation='softmax')\n])\n<\/code><\/pre>\n<h2>3. Compile and Train the Model<\/h2>\n<p>Compile the model with the Adam optimizer, sparse categorical crossentropy loss (for integer labels), and accuracy metric. Train for 5 epochs with a batch size of 32.<\/p>\n<pre><code>model.compile(optimizer='adam',\n              loss='sparse_categorical_crossentropy',\n              metrics=['accuracy'])\nhistory = model.fit(x_train, y_train, epochs=5, batch_size=32, validation_split=0.2)\n<\/code><\/pre>\n<h2>4. Evaluate on Test Data<\/h2>\n<p>After training, evaluate the model on the test set to measure real-world performance.<\/p>\n<pre><code>test_loss, test_acc = model.evaluate(x_test, y_test)\nprint(f\"Test accuracy: {test_acc:.4f}\")\n<\/code><\/pre>\n<h2>Conclusion<\/h2>\n<p>You\u2019ve built and trained a simple neural network with Keras in just a few lines. From here, experiment with more layers, different activation functions, or try a convolutional network for image data. Keras makes deep learning accessible\u2014happy coding!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>How to Build a Simple Neural Network with Keras: A Step-by-Step Tutorial Keras, now part of TensorFlow, is one of the easiest deep learning frameworks to get started with. In this tutorial, you&#8217;ll build a feedforward neural network for classifying handwritten digits from the MNIST dataset. You only need basic Python knowledge and a few &hellip; <\/p>\n","protected":false},"author":2716,"featured_media":2351,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"om_disable_all_campaigns":false,"_monsterinsights_skip_tracking":false,"footnotes":""},"categories":[1],"tags":[],"class_list":["post-2352","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-non-category"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/sumberlaba.com\/index.php\/wp-json\/wp\/v2\/posts\/2352","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/sumberlaba.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/sumberlaba.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/sumberlaba.com\/index.php\/wp-json\/wp\/v2\/users\/2716"}],"replies":[{"embeddable":true,"href":"https:\/\/sumberlaba.com\/index.php\/wp-json\/wp\/v2\/comments?post=2352"}],"version-history":[{"count":1,"href":"https:\/\/sumberlaba.com\/index.php\/wp-json\/wp\/v2\/posts\/2352\/revisions"}],"predecessor-version":[{"id":2353,"href":"https:\/\/sumberlaba.com\/index.php\/wp-json\/wp\/v2\/posts\/2352\/revisions\/2353"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/sumberlaba.com\/index.php\/wp-json\/wp\/v2\/media\/2351"}],"wp:attachment":[{"href":"https:\/\/sumberlaba.com\/index.php\/wp-json\/wp\/v2\/media?parent=2352"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/sumberlaba.com\/index.php\/wp-json\/wp\/v2\/categories?post=2352"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/sumberlaba.com\/index.php\/wp-json\/wp\/v2\/tags?post=2352"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}