{"id":697464,"date":"2026-04-07T11:36:35","date_gmt":"2026-04-07T10:36:35","guid":{"rendered":"https:\/\/blog.jetbrains.com\/?post_type=pycharm&#038;p=697464"},"modified":"2026-05-04T11:07:08","modified_gmt":"2026-05-04T10:07:08","slug":"how-to-train-your-first-tensorflow-model","status":"publish","type":"pycharm","link":"https:\/\/blog.jetbrains.com\/pt-br\/pycharm\/2026\/04\/how-to-train-your-first-tensorflow-model","title":{"rendered":"How to Train Your First TensorFlow Model in PyCharm"},"content":{"rendered":"\n<p><em>This is a guest post from <\/em><strong><em><a href=\"https:\/\/blog.jetbrains.com\/pycharm\/2026\/04\/how-to-train-your-first-tensorflow-model\/#author\" data-type=\"link\" data-id=\"https:\/\/blog.jetbrains.com\/pycharm\/2026\/04\/how-to-train-your-first-tensorflow-model\/#author\">Iulia Feroli<\/a><\/em><\/strong><em>, founder of the Back To Engineering community on YouTube.<\/em><\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img decoding=\"async\" loading=\"lazy\" width=\"1280\" height=\"720\" src=\"https:\/\/blog.jetbrains.com\/wp-content\/uploads\/2026\/04\/PC-social-BlogFeatured-1280x720-1-1.png\" alt=\"How to Train Your First TensorFlow Model in PyCharm\" class=\"wp-image-697465\"\/><\/figure>\n\n\n\n<p><a href=\"https:\/\/www.tensorflow.org\/\" target=\"_blank\" rel=\"noopener\">TensorFlow<\/a> is a powerful open-source framework for building machine learning and deep learning systems. At its core, it works with tensors (a.k.a multi\u2011dimensional arrays) and provides high\u2011level libraries (like Keras) that make it easy to transform raw data into models you can train, evaluate, and deploy.<\/p>\n\n\n\n<p>TensorFlow helps you handle the full pipeline: loading and preprocessing data, assembling models from layers and activations, training with optimizers and loss functions, and exporting for serving or even running on edge devices (including lightweight TensorFlow Lite models on Raspberry Pi and other microcontrollers).&nbsp;<\/p>\n\n\n\n<p>If you want to make data-driven applications, prototyping neural networks, or ship models to production or devices, learning TensorFlow gives you a consistent, well-supported toolkit to go from idea to deployment.<\/p>\n\n\n\n<p>If you\u2019re brand new to TensorFlow, start by watching the <strong><a href=\"https:\/\/www.youtube.com\/watch?v=hm07b8ETaso\" data-type=\"link\" data-id=\"https:\/\/www.youtube.com\/watch?v=hm07b8ETaso\" target=\"_blank\" rel=\"noopener\">short overview video<\/a><\/strong> where I explain tensors, neural networks, layers, and why TensorFlow is great for taking data \u2192 model \u2192 deployment, and how all of this can be explained with a LEGO-style pieces sorting example.&nbsp;<\/p>\n\n\n\n<p>In this blog post, I\u2019ll walk you through a first, stripped-down TensorFlow implementation notebook so we can get started with some practical experience. You can also watch the walkthrough video to follow along.<\/p>\n\n\n\n<figure class=\"wp-block-embed is-type-video is-provider-youtube wp-block-embed-youtube wp-embed-aspect-16-9 wp-has-aspect-ratio\"><div class=\"wp-block-embed__wrapper\">\n<iframe loading=\"lazy\" title=\"Build Your First TensorFlow Model in Python (A Step-by-Step Tutorial)\" width=\"500\" height=\"281\" src=\"https:\/\/www.youtube.com\/embed\/nswGrvOhaOY?feature=oembed\" frameborder=\"0\" allow=\"accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share\" referrerpolicy=\"strict-origin-when-cross-origin\" allowfullscreen><\/iframe>\n<\/div><\/figure>\n\n\n\n<p>We&#8217;ll be exploring a very simple use case today: load the Fashion MNIST dataset, build two very simple Keras models, train and compare them, then dig into visualizations (predictions, confidence bars, confusion matrix). I kept the code minimal and readable so you can focus on the ideas \u2013 and you\u2019ll see how <a href=\"https:\/\/www.jetbrains.com\/pycharm\/data-science\/\" target=\"_blank\" rel=\"noopener\">PyCharm<\/a> helps along the way.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Training TensorFlow models step by step<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">Getting started in PyCharm<\/h3>\n\n\n\n<p>We&#8217;ll be leveraging PyCharm&#8217;s native Notebook integration to build out <a href=\"https:\/\/github.com\/iuliaferoli\/TensorFlow_with_pycharm\" target=\"_blank\" rel=\"noopener\">our project<\/a>. This way, we can inspect each step of the pipeline and use some supporting visualization along the way. We&#8217;ll <a href=\"https:\/\/www.jetbrains.com\/help\/pycharm\/creating-empty-project.html\" target=\"_blank\" rel=\"noopener\">create a new project<\/a> and <a href=\"https:\/\/www.jetbrains.com\/help\/pycharm\/creating-virtual-environment.html\" target=\"_blank\" rel=\"noopener\">generate a virtual environment<\/a> to manage our dependencies.&nbsp;<\/p>\n\n\n\n<p>If you&#8217;re running the code from the attached repo, you can install directly from the requirements file. If you wish to expand this example with additional visualizations for further models, you can easily add more packages to your requirements as you go by using the PyCharm package manager helpers for <a href=\"https:\/\/www.jetbrains.com\/guide\/python\/tips\/install-and-import\/)%20and\" target=\"_blank\" rel=\"noopener\">installing<\/a> and <a href=\"https:\/\/www.jetbrains.com\/help\/pycharm\/installing-uninstalling-and-upgrading-packages.html\" target=\"_blank\" rel=\"noopener\">upgrading<\/a>.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Load <code>Fashion MNIST<\/code> and inspect the data<\/h3>\n\n\n\n<p><code>Fashion MNIST<\/code> is a great starter because the images are small (28\u00d728 pixels), visually meaningful, and easy to interpret. They represent various garment types as pixelated black-and-white images, and provide the relevant labels for a well-contained classification task. We can first take a look at our data sample by printing some of these images with various matplotlib functions:<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img decoding=\"async\" loading=\"lazy\" width=\"795\" height=\"342\" src=\"https:\/\/blog.jetbrains.com\/wp-content\/uploads\/2026\/04\/image1.png\" alt=\"\" class=\"wp-image-699830\"\/><\/figure>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">```\nfig, axes = plt.subplots(2, 5, figsize=(10, 4))\nfor i, ax in enumerate(axes.flat):\n    ax.imshow(x_train[i], cmap='gray')\n    ax.set_title(class_names[y_train[i]])\n    ax.axis('off')\nplt.show()\n```\n# Two simple models (a quick experiment)\n```\nmodel1 = models.Sequential([\n    layers.Flatten(input_shape=(28, 28)),\n    layers.Dense(128, activation='relu'),\n    layers.Dense(10, activation='softmax')\n])\nmodel2 = models.Sequential([\n    layers.Flatten(input_shape=(28, 28)),\n    layers.Dense(128, activation='relu'),\n    layers.Dense(128, activation='relu'),\n    layers.Dense(10, activation='softmax')\n])\n```<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Compile and train your first model<\/h3>\n\n\n\n<p>From here, we can compile and train our first TensorFlow model(s). With PyCharm\u2019s code completion features and documentation access, you can get instant suggestions for building out these simple code blocks.<\/p>\n\n\n\n<p>For a first try at TensorFlow, this allows us to spin up a working model with just a few presses of <em>Tab<\/em> in our IDE. We&#8217;re using the recommended standard optimizer and loss function, and we&#8217;re tracking for accuracy. We can choose to build multiple models by playing around with the number or type of layers, along with the other parameters.&nbsp;<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">```\nmodel1.compile(\n    optimizer='adam',\n    loss='sparse_categorical_crossentropy',\n    metrics=['accuracy']\n)\nmodel1.fit(x_train, y_train, epochs=10)\nmodel2.compile(\n    optimizer='adam',\n    loss='sparse_categorical_crossentropy',\n    metrics=['accuracy']\n)\nmodel2.fit(x_train, y_train, epochs=15)\n```<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Evaluate and compare your TensorFlow model performance<\/h3>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">```\nloss1, accuracy1 = model1.evaluate(x_test, y_test)\nprint(f'Accuracy of model1: {accuracy1:.2f}')\nloss2, accuracy2 = model2.evaluate(x_test, y_test)\nprint(f'Accuracy of model2: {accuracy2:.2f}')\n```<\/pre>\n\n\n\n<p>Once the models are trained (and you can see the epochs progressing visually as each cell is run), we can immediately evaluate the performance of the models.<\/p>\n\n\n\n<p>In my experiment, <code>model1<\/code> sits around ~0.88 accuracy, and while <code>model2<\/code> is a little higher than that, it took 50% longer to train. That\u2019s the kind of trade\u2011off you should be thinking about: Is a tiny accuracy gain worth the additional compute and complexity?&nbsp;<\/p>\n\n\n\n<p>We can dive further into the results of the model run by generating a DataFrame instance of our new prediction dataset. Here we can also leverage built-in functions like `describe` to quickly get some initial statistical impressions:<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img decoding=\"async\" loading=\"lazy\" width=\"673\" height=\"656\" src=\"https:\/\/blog.jetbrains.com\/wp-content\/uploads\/2026\/04\/image2.png\" alt=\"\" class=\"wp-image-699841\"\/><\/figure>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">```\npredictions = model1.predict(x_test)\nimport pandas as pd\ndf_pred = pd.DataFrame(predictions, columns=class_names)\ndf_pred.describe()\n```<\/pre>\n\n\n\n<p>However, the most useful statistics will compare our model&#8217;s prediction with the ground truth &#8220;real&#8221; labels of our dataset. We can also break this down by item category:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">```\ny_pred = model1.predict(x_test).argmax(axis=1)\ncm = confusion_matrix(y_test, y_pred)\nplt.figure(figsize=(8,6))\nsns.heatmap(cm, annot=True, fmt='d', cmap='Blues', xticklabels=class_names, yticklabels=class_names)\nplt.xlabel('Predicted')\nplt.ylabel('True')\nplt.title('Confusion Matrix')\nplt.show()\nprint('Classification report:')\nprint(classification_report(y_test, y_pred, target_names=class_names))\n```<\/pre>\n\n\n\n<p>From here, we can notice that the accuracy differs quite a bit by type of garment. A possible interpretation of this is that trousers are quite a distinct type of clothing from, say, t-shirts and shirts, which can be more commonly confused.&nbsp;<\/p>\n\n\n\n<p>This is, of course, the type of nuance that, as humans, we can pick up by looking at the images, but the model only has access to a matrix of pixel values. The data does seem, however, to confirm our intuition. We can further build a more comprehensive visualization to test this hypothesis.&nbsp;<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img decoding=\"async\" loading=\"lazy\" width=\"818\" height=\"1600\" src=\"https:\/\/blog.jetbrains.com\/wp-content\/uploads\/2026\/04\/image-4.png\" alt=\"\" class=\"wp-image-697493\"\/><\/figure>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">```\nimport numpy as np\nimport matplotlib.pyplot as plt\n# pick 8 wrong examples\ny_pred = predictions.argmax(axis=1)\nwrong_idx = np.where(y_pred != y_test)[0][:8]  # first 8 mistakes\nn = len(wrong_idx)\nfig, axes = plt.subplots(n, 2, figsize=(10, 2.2 * n), constrained_layout=True)\nfor row, idx in enumerate(wrong_idx):\n    p = predictions[idx]\n    pred = int(np.argmax(p))\n    true = int(y_test[idx])\n    axes[row, 0].imshow(x_test[idx], cmap=\"gray\")\n    axes[row, 0].axis(\"off\")\n    axes[row, 0].set_title(\n        f\"WRONG  P:{class_names[pred]} ({p[pred]:.2f})  T:{class_names[true]}\",\n        color=\"red\",\n        fontsize=10\n    )\n    bars = axes[row, 1].bar(range(len(class_names)), p, color=\"lightgray\")\n    bars[pred].set_color(\"red\")\n    axes[row, 1].set_ylim(0, 1)\n    axes[row, 1].set_xticks(range(len(class_names)))\n    axes[row, 1].set_xticklabels(class_names, rotation=90, fontsize=8)\n    axes[row, 1].set_ylabel(\"conf\", fontsize=9)\nplt.show()\n```<\/pre>\n\n\n\n<p>This table generates a view where we can explore the confidence our model had in a prediction: By exploring which weight each class was given, we can see where there was doubt (i.e. multiple classes with a higher weight) versus when the model was certain (only one guess). These examples further confirm our intuition: top-types appear to be more commonly confused by the model.&nbsp;<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Conclusion<\/h2>\n\n\n\n<p>And there we have it! We were able to set up and train our first model and already drive some data science insights from our data and model results. Using some of the PyCharm functionalities at this point can speed up the experimentation process by providing access to our documentation and applying code completion directly in the cells. We can even use AI Assistant to help generate some of the graphs we&#8217;ll need to further evaluate the TensorFlow model performance and investigate our results.<\/p>\n\n\n\n<p>You can <a href=\"https:\/\/github.com\/iuliaferoli\/TensorFlow_with_pycharm\" target=\"_blank\" rel=\"noopener\">try out this notebook yourself<\/a>, or better yet, try to generate it with these same tools for a more hands-on learning experience.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Where to go next<\/h2>\n\n\n\n<p><a href=\"https:\/\/github.com\/iuliaferoli\/TensorFlow_with_pycharm\" target=\"_blank\" rel=\"noopener\">This notebook<\/a> is a minimal, teachable starting point. Here are some practical next steps to try afterwards:<\/p>\n\n\n\n<ul>\n<li>Replace the dense baseline with a small CNN (Conv2D \u2192 MaxPooling \u2192 Dense).<\/li>\n\n\n\n<li>Add dropout or batch normalization to reduce overfitting.<\/li>\n\n\n\n<li>Apply data augmentation (random shifts\/rotations) to improve generalization.<\/li>\n\n\n\n<li>Use callbacks like <code>EarlyStopping<\/code> and <code>ModelCheckpoint<\/code> so training is efficient, and you keep the best weights.<\/li>\n\n\n\n<li>Export a <code>SavedModel<\/code> for server use or convert to TensorFlow Lite for edge devices (Raspberry Pi, microcontrollers).<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Frequently asked questions<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">When should I use TensorFlow?<\/h3>\n\n\n\n<p>TensorFlow is best used when building machine learning or deep learning models that need to scale, go into production, or run across different environments (cloud, mobile, edge devices).&nbsp;<\/p>\n\n\n\n<p>TensorFlow is particularly well-suited for large-scale models and neural networks, including scenarios where you need strong deployment support (TensorFlow Serving, TensorFlow Lite). For research prototypes, TensorFlow is viable, but it\u2019s more commonplace to use lightweight frameworks for easier experimentation.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Can TensorFlow run on a GPU?<\/h3>\n\n\n\n<p>Yes, TensorFlow can run GPUs and TPUs. Additionally, using a GPU can significantly speed up training, especially for deep learning models with large datasets. The best part is, TensorFlow will automatically use an available GPU if it\u2019s properly configured.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">What is loss in TensorFlow?<\/h3>\n\n\n\n<p>Loss (otherwise known as loss function) measures how far a model\u2019s predictions are from the actual target values. Loss in TensorFlow is a numerical value representing the distance between predictions and actual target values. A few examples include:&nbsp;<\/p>\n\n\n\n<ul>\n<li>MSE (mean squared error), used in regression tasks.<\/li>\n\n\n\n<li>Cross-entropy loss, often used in classification tasks.<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">How many epochs should I use?<\/h3>\n\n\n\n<p>There\u2019s no set number of epochs to use, as it depends on your dataset and model. Typical approaches cover:&nbsp;<\/p>\n\n\n\n<ul>\n<li>Starting with a conservative number (10\u201350 epochs).<\/li>\n\n\n\n<li>Monitoring validation loss\/accuracy and adjusting based on the results you see.<\/li>\n\n\n\n<li>Using early stopping to halt training when improvements decrease.<\/li>\n<\/ul>\n\n\n\n<p>An epoch is one full pass through your training data. Not enough passes through leads to underfitting, and too many can cause overfitting. The sweet spot is where your model generalizes best to unseen data.&nbsp;<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"author\">About the author<\/h2>\n\n\n    <div class=\"about-author \">\n        <div class=\"about-author__box\">\n            <div class=\"row\">\n                                                            <div class=\"about-author__box-img\">\n                            <img decoding=\"async\" src=\"https:\/\/blog.jetbrains.com\/wp-content\/uploads\/2026\/04\/Iulia-Feroli-e1775558363746.png\" alt=\"\" loading=\"lazy\">\n                        <\/div>\n                                        <div class=\"about-author__box-text\">\n                                                    <h4>Iulia Feroli<\/h4>\n                                                <p><span style=\"font-weight: 400;\">Iulia\u2019s mission is to make tech exciting, understandable, and accessible to the new generation.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">With a background spanning data science, AI, cloud architecture, and open source, she brings a unique perspective on bridging technical depth with approachability.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">She\u2019s building her own brand, Back To Engineering, through which she creates a community for tech enthusiasts, engineers, and makers. From YouTube videos on building robots from scratch, to conference talks or keynotes about real, grounded AI, and technical blogs and tutorials <\/span><span style=\"font-weight: 400;\">\u2013<\/span><span style=\"font-weight: 400;\"> Iulia shares her message worldwide on how to turn complex concepts into tools developers can use every day.<\/span><\/p>\n                    <\/div>\n                            <\/div>\n        <\/div>\n    <\/div>\n\n\n\n<p><\/p>\n","protected":false},"author":1223,"featured_media":697465,"comment_status":"closed","ping_status":"closed","template":"","categories":[952,2347],"tags":[6416,9088],"cross-post-tag":[],"acf":[],"_links":{"self":[{"href":"https:\/\/blog.jetbrains.com\/pt-br\/wp-json\/wp\/v2\/pycharm\/697464"}],"collection":[{"href":"https:\/\/blog.jetbrains.com\/pt-br\/wp-json\/wp\/v2\/pycharm"}],"about":[{"href":"https:\/\/blog.jetbrains.com\/pt-br\/wp-json\/wp\/v2\/types\/pycharm"}],"author":[{"embeddable":true,"href":"https:\/\/blog.jetbrains.com\/pt-br\/wp-json\/wp\/v2\/users\/1223"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.jetbrains.com\/pt-br\/wp-json\/wp\/v2\/comments?post=697464"}],"version-history":[{"count":9,"href":"https:\/\/blog.jetbrains.com\/pt-br\/wp-json\/wp\/v2\/pycharm\/697464\/revisions"}],"predecessor-version":[{"id":699863,"href":"https:\/\/blog.jetbrains.com\/pt-br\/wp-json\/wp\/v2\/pycharm\/697464\/revisions\/699863"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/blog.jetbrains.com\/pt-br\/wp-json\/wp\/v2\/media\/697465"}],"wp:attachment":[{"href":"https:\/\/blog.jetbrains.com\/pt-br\/wp-json\/wp\/v2\/media?parent=697464"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.jetbrains.com\/pt-br\/wp-json\/wp\/v2\/categories?post=697464"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.jetbrains.com\/pt-br\/wp-json\/wp\/v2\/tags?post=697464"},{"taxonomy":"cross-post-tag","embeddable":true,"href":"https:\/\/blog.jetbrains.com\/pt-br\/wp-json\/wp\/v2\/cross-post-tag?post=697464"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}