Posts

Showing posts with the label TensorFlow

LSTM-Demo: Bidirectional LSTM with TensorFlow on the Sonar Dataset

Image
LSTM-Demo is a compact, runnable example that trains a bidirectional Long Short-Term Memory (LSTM) network with TensorFlow and Keras on the classic Sonar dataset. The Sonar dataset is a well-known binary classification benchmark: each sample has 60 numeric features from sonar returns, and the task is to tell metal cylinders (mines) from rocks. It is small enough to train quickly while still showing a full pipeline. A bidirectional LSTM reads the input sequence in both directions, which can help the model use context from the whole sequence. The demo is intended as a teaching example you can run, read, and modify. It is archived on Zenodo with a citable DOI. DOI: https://doi.org/10.5281/zenodo.20672929

[SOLVED] Tuple index out of range in TensorFlow model fit function

Image
model.fit(): list vs NumPy array # Problem: plain Python lists model.fit(x_train, y_train) IndexError: tuple index out of range # Fix: wrap with np.array() model.fit(np.array(x_train), np.array(y_train)) training runs When training a Keras or TensorFlow model you may hit an IndexError: tuple index out of range the moment you call model.fit() , even though your data looks fine. The cause is almost always the type of the data you passed in, not the model. This post explains why it happens and the one-line fix. The problem code Here training data is built up as plain Python lists and passed straight to fit : x_train = [] y_train = [] # ... append samples to the lists ... history = model.fit(x_train, y_train, batch_size=batch_size, epochs=epochs) This raises: IndexError: tuple index out of range Why it happens Keras needs to know the shape of your input to wire up the first layer and to split data into batches. It reads that shape from the .shape attribute of the a...

[How To] Run Bidirectional LSTM in Python

Image
 This is a sample program to run bidirectional LSTM in Python using Tensorflow. The complete code repository is available at  https://github.com/devharsh/LSTM-Demo . Code: #!/usr/bin/env python3 # -*- coding: utf-8 -*- """ Created on Wed Jun 1 13:34:00 2022. @author: devharsh """ import matplotlib.pyplot as plt from pandas import read_csv from sklearn.model_selection import train_test_split from sklearn.preprocessing import LabelEncoder from tensorflow import keras from tensorflow.keras import layers inputs = keras.Input(shape=(None,), dtype="float64") xem = layers.Embedding(101, 128)(inputs) xl1 = layers.Bidirectional(layers.LSTM(64, return_sequences=True))(xem) xl2 = layers.Bidirectional(layers.LSTM(64))(xl1) outputs = layers.Dense(1, activation="sigmoid")(xl2) model = keras.Model(inputs, outputs) model.summary()

Install TensorFlow 1.8 on Raspberry Pi 3 Model B+ [Complete Instruction Manual 2018]

Image
sudo apt-get update sudo apt-get upgrade sudo apt-get dist-upgrade sudo apt-get install python-pip python-dev wget https://www.piwheels.org/simple/tensorflow/tensorflow-1.8.0-cp27-none-linux_armv7l.whl sudo pip install tensorflow-1.8.0-cp27-none-linux_armv7l.whl sudo pip uninstall mock sudo pip install mock /* Test program HelloTensor.py */ sudo pip install pillow sudo pip install lxml sudo pip install jupyter sudo pip install matplotlib sudo pip install protobuf git clone https://github.com/tensorflow/models.git cd models cd research protoc object_detection/protos/*.proto --python_out=. export PYTHONPATH=$PYTHONPATH:`pwd`:`pwd`/slim /* If Protobuf is throwing errors: sudo apt-get install autoconf automake libtool curl make g++ unzip wget https://github.com/google/protobuf/releases/download/v3.6.0/protobuf-cpp-3.6.0.tar.gz tar -xvf protobuf-cpp-3.2.0.tar.gz cd protobuf-3.2.0 ./autogen.sh ./configure make make check sudo make install sudo ldconfig */...