I am making use of pybrain to build a network that has 6 input dimensions and one real valued output dimension. The code I use is shown below:
network = buildNetwork(train.indim, 4, train.outdim)
trainer = BackpropTrainer( network, train)
trainer.trainOnDataset(train, 8000)
print 'MSE train', trainer.testOnData(train, verbose = True)
here train is of type Dataset
I want to get the predictions made in trainer.testOnData() as a numpy array. I am able to view the predicted result along with the error but I want it as an array. Is there anyway that this can be done?
Use the activate function of your network:
numpy.array([network.activate(x) for x, _ in train])
Complete example:
from datasets import XORDataSet
from pybrain.tools.shortcuts import buildNetwork
from pybrain.supervised import BackpropTrainer
import numpy
d = XORDataSet()
n = buildNetwork(d.indim, 4, d.outdim, bias=True)
t = BackpropTrainer(n, learningrate=0.01, momentum=0.99, verbose=True)
t.trainOnDataset(d, 1000)
t.testOnData(verbose=True)
print numpy.array([n.activate(x) for x, _ in d])
(Only works in the directory pybrain/examples/supervised/backprop of pybrain because the XORDataSet is required.)
Related
The post is directed to user's who have used the library deepxde. May you kindly help me in finding the solution to my problem.
I tried the DeepOnet operator learning example according to the dataset generated from grf from this tutorial website link . Now I want to apply the trained model on a known pair of function like cos(x) and sin(x) ; I'll generate my input data-field data as x = np.arange(0,4*np.pi,0.1) ; u(y) = y = np.cos(x) now I want to plot the anti-derivate of my u(y) and compare it with y = np.cos(x).
What additional code shall I implement in order to do so?
The following code runs well on my colab after installing the deepxde library with pip. Now I just want to extend this and check whether the trained model works okay on my custom fucntions or not? for example cosx , sinx etc. Thanks for the help!
import deepxde as dde
import matplotlib.pyplot as plt
import numpy as np
# Load dataset
d = np.load("antiderivative_aligned_train.npz", allow_pickle=True)
X_train = (d["X"][0].astype(np.float32), d["X"][1].astype(np.float32))
y_train = d["y"].astype(np.float32)
d = np.load("antiderivative_aligned_test.npz", allow_pickle=True)
X_test = (d["X"][0].astype(np.float32), d["X"][1].astype(np.float32))
y_test = d["y"].astype(np.float32)
data = dde.data.TripleCartesianProd(
X_train=X_train, y_train=y_train, X_test=X_test, y_test=y_test
)
# Choose a network
m = 100
dim_x = 1
net = dde.nn.DeepONetCartesianProd(
[m, 40, 40],
[dim_x, 40, 40],
"relu",
"Glorot normal",
)
# Define a Model
model = dde.Model(data, net)
# Compile and Train
model.compile("adam", lr=0.001, metrics=["mean l2 relative error"])
losshistory, train_state = model.train(iterations=10000)
# Plot the loss trajectory
dde.utils.plot_loss_history(losshistory)
plt.show()
I'm trying to put a collection of images through a neural network, but I can't figure out how to get a large collection of images to go into a tensorflow model, as trying to convert the collection into a numpy array causes a memory error.
I should note that I am very new to tensorflow.
import numpy as np
from skimage.io import imread_collection
from tensorflow import keras
from tensorflow.keras import layers
def gen(arr):return(i.reshape(400*600*3) for i in arr) # Only used in Attempt2.
labelFile=open("lables_text_file.txt","r")
labels=labelFile.read()
labelFile.close()
labels=getTrain(labels)#Converts to a tuple containing the lables in order.
data = imread_collection("path_to_images/*.jpg", conserve_memory=True)
train=data[:-len(data)//4]
trainLabels=labels[:-len(data)//4]
test=data[-len(data)//4:]
testLabels=labels[-len(data)//4:]
#train = train.reshape(-1, 400*600*3) # Attempt1
#test = test.reshape(-1, 400*600*3) # Attempt1
#train = gen(train) # Attempt2
#test = gen(test) # Attempt2
trainLabels = keras.utils.to_categorical(trainLabels, 23)
testLabels = keras.utils.to_categorical(testLabels, 23)
model=keras.Sequential([keras.Input(shape=(400*600*3,)),
layers.Dense(600, name='hidden1', activation='relu'),
layers.Dense(400, name='hidden2', activation='relu'),
layers.Dense(46, name='hidden3', activation='relu'),
layers.Dense(23, activation="softmax")])
optimizer = keras.optimizers.Adam(learning_rate=0.0015)
model.compile(loss=keras.losses.CategoricalCrossentropy(), optimizer=optimizer, metrics=[keras.metrics.CategoricalAccuracy()])
model.fit(train,trainLabels,batch_size=128,epochs=8,validation_data=(test,testLabels), shuffle=True)
When I run the code as is, this is the result:
ValueError: Failed to find data adapter that can handle input: <class 'skimage.io.collection.ImageCollection'>, <class 'numpy.ndarray'>
When I try to use Attempt1, this is the result:
AttributeError: 'ImageCollection' object has no attribute 'reshape'
When I try to use Attempt2, this is the result:
ValueError: `y` argument is not supported when using python generator as input.
How can I put the data into `model.fit, such that it will successfully train the neural network?
I think I may have solved the problems.
Working code:
import numpy as np
from skimage.io import imread_collection
from tensorflow import keras
from tensorflow.keras import layers
def gen(arr,labels):return((arr[i].reshape(-1,400*600*3),labels[i].reshape(-1,23)) for i in range(len(arr)))
labelFile=open("lables_text_file.txt","r")
labels=labelFile.read()
labelFile.close()
labels=getTrain(labels)#Converts to a tuple containing the lables in order.
data = imread_collection("path_to_images/*.jpg", conserve_memory=True)
train=data[:-len(data)//4]
trainLabels=labels[:-len(data)//4]
test=data[-len(data)//4:]
testLabels=labels[-len(data)//4:]
#train = train.reshape(-1, 400*600*3) # Attempt1
#test = test.reshape(-1, 400*600*3) # Attempt1
trainLabels = keras.utils.to_categorical(trainLabels, 23)
testLabels = keras.utils.to_categorical(testLabels, 23)
train = gen(train,trainLabels) # Attempt2
test = gen(test,testLabels) # Attempt2
model=keras.Sequential([keras.Input(shape=(400*600*3,)),
layers.Dense(600, name='hidden1', activation='relu'),
layers.Dense(400, name='hidden2', activation='relu'),
layers.Dense(46, name='hidden3', activation='relu'),
layers.Dense(23, activation="softmax")])
optimizer = keras.optimizers.Adam(learning_rate=0.0015)
model.compile(loss=keras.losses.CategoricalCrossentropy(), optimizer=optimizer, metrics=[keras.metrics.CategoricalAccuracy()])
model.fit(train,None,batch_size=128,epochs=8,validation_data=(test,testLabels), shuffle=True)
The solution was to pass in a generator that returns two-tuples containing the input and label (instead of passing the labels in directly), but there were other problems that I may include in this answer if I get the time.
I'm using tf 1.15, i'm trying to make a regression task using a signal.
First of all i load my signals into the pipeline, i have several files, here i simulate the loading using a np.zeros to make the code usable by you.
Every file has this shape (?, 75000, 3), where ? is a random number of elements, 75000 is the number of samples in each element and 3 is the number of signals.
Using the tf.data i unpack them and i get a dataset who output signals with this shape (75000,), and i use them in my keras model.
Everything should be fine until i create the keras model, i copied my input pipeline because during my tests i got different errors using a generic tf.data.dataset or using the dataset built in this way.
import numpy as np
import tensorflow as tf
# called in the dataset pipeline
def my_func(x):
p = np.zeros([86, 75000, 3])
x = p[:,:,0]
y = p[:, :, 1]
z = p[:, :, 2]
return x, y, z
# called in the dataset pipeline
def load_sign(path):
func = tf.compat.v1.numpy_function(my_func, [path], [tf.float64, tf.float64, tf.float64])
return func
# Dataset pipeline
s = [1, 2] # here i have the file paths, i simulate it with numbers
AUTOTUNE = tf.data.experimental.AUTOTUNE
ds = tf.data.Dataset.from_tensor_slices(s)
# ds = ds.map(load_sign, num_parallel_calls=AUTOTUNE)
ds = ds.map(load_sign, num_parallel_calls=AUTOTUNE).unbatch()
itera = tf.data.make_one_shot_iterator(ds)
ABP, ECG, PLETH = itera.get_next()
# Until there everything should be fine
# Here i create my convolutional network
signal = tf.keras.layers.Input(shape=(None,75000), dtype='float32')
x = tf.compat.v1.keras.layers.Conv1D(64, (1), strides=1, padding='same')(signal)
x = tf.keras.layers.Dense(75000)(x)
model = tf.keras.Model(inputs=signal, outputs=x, name='resnet18')
# And finally i try to insert my signal into model
logits = model(PLETH)
I get this error:
ValueError: Input 0 of layer conv1d is incompatible with the layer: its rank is undefined, but the layer requires a defined rank.
Why? And how can i make it works?
Also the input size of my net should be this one according the documentation:
3D tensor with shape: (batch_size, steps, input_dim)
What is the steps? In my case i assume it should be (batch_size, 1, 75000), right?
from pybrain.structure import FeedForwardNetwork
from pybrain.structure import LinearLayer, SigmoidLayer
from pybrain.structure import FullConnection
from pybrain.datasets import SupervisedDataSet
import numpy as np
X = np.loadtxt('xdatanorm.txt', dtype=float)
y = np.loadtxt('ydatanorm.txt', dtype=float)
n = FeedForwardNetwork()
inLayer = LinearLayer(35)
hiddenLayer = SigmoidLayer(18)
outLayer = LinearLayer(1)
n.addInputModule(inLayer)
n.addModule(hiddenLayer)
n.addOutputModule(outLayer)
in_to_hidden = FullConnection(inLayer, hiddenLayer)
hidden_to_out = FullConnection(hiddenLayer, outLayer)
n.addConnection(in_to_hidden)
n.addConnection(hidden_to_out)
n.sortModules()
DS = SupervisedDataSet(35,1)
DS.addSample(X,y)
Starting using pybrain to get a Neural Network to work on my diffusion energy data. I don't know how to get a dataset working from my X and y values. X is 35 inputs and y is 1 ouput and there are 148 samples. With this code I get the error: "ValueError: could not broadcast input array from shape (148,35) into shape (35)"
Need to know how to properly prepare a dataset for pybrain.
I believe the .addSample() method expects one sample at a time. Rather than using .addSample(), try
assert(X.shape[0] == y.shape[0])
DS.setField('input', X)
DS.setField('target', y)
The 'assert()' is recommended because the .setField() method does not verify array dimensions like .addSample() does.
See Pybrain dataset tutorial for more info.
Hey guys I need a bit of help with my pybrain code. Everything loads fine, but after it trains the first time the training error doesn't go down. In fact, it just stays stuck there at exactly 13.3484055174. I've been checking my code many times and comparing it with other examples, but I consistently get the same problem. I've also already tried changing the number of hidden units, learningrate, momentum, weightdecay to no avail. I've checked the parameters and it starts off from [-1 to 1] then blows up into ~240-250. I was wondering if anyone can see why it's not working. I'm sure it's a really simple 1-liner that I'm missing.
I'm working on the kaggle 0-9 digit classification dataset. I've already gotten the randomforest to work but I really want to make this neural network work too. Any help would get greatly appreciated.
#learn digit classification with a nerual network
import pybrain
from pybrain.datasets import *
from pybrain.tools.shortcuts import buildNetwork
from pybrain.supervised.trainers import BackpropTrainer
from pybrain.structure.modules import SoftmaxLayer
from pybrain.utilities import percentError
import numpy
print "Importing training and test data"
data = numpy.genfromtxt('trainR.csv', delimiter = ',')
data = data[1:]
traindata = data[:(len(data)/2)]
testdata = data[(len(data)/2)+1:]
print "Importing actual data"
actualdata = numpy.genfromtxt('trainR.csv', delimiter = ',')
print "Adding samples to dataset and setting up neural network"
ds = ClassificationDataSet(784, 10, nb_classes = 10)
for x in traindata:
ds.addSample(tuple(x[1:]),tuple(x[0:1]))
ds._convertToOneOfMany( bounds=[0,1] )
net = buildNetwork(784, 100, 10, bias=True, outclass=SoftmaxLayer)
print "Training the neural network"
trainer = BackpropTrainer(net, dataset=ds, momentum = 0.1,
verbose = True, weightdecay = 0.01)
for i in range(3):
# train the network for 1 epoch
trainer.trainEpochs( 1 )
# evaluate the result on the training and test data
trnresult = percentError( trainer.testOnClassData(), [x[0] for x in traindata] )
# print the result
print "epoch: " + str(trainer.totalepochs) + " train error: " + str(trnresult)
print ""
print "Predicting with the neural network"
answerlist = []
for row in testdata:
answer = numpy.argmax(net.activate(row[1:]))
answerlist.append(answer)
tstresult = percentError(answerlist, [x[0] for x in testdata])
print "Test error: " + str(tstresult)
try changing
ds = ClassificationDataSet(784, 10, nb_classes = 10)
to
ds = ClassificationDataSet(784, 1, nb_classes = 10)
i think ClassificationDataSet second argument is the dimensions of the targets rather than the number of classes this is given by nb_classes. It depends on how your data is organized. Best thing is to enter each target as an integer for each class and then use _convertToOneOfMany()
It would be useful if you provided your first sample