Perceptron fitting error (UFuncTypeError) - machine-learning

I try to fit the iris dataset with this Perceptron class but I got an error in fitting
Jupyter notebook snapshot
for the .ipynb file:
https://mega.nz/file/m25R2QZb#21OKd7DTASEmOymuFcOiOQwZaf8fhMzHLeQc8XzyKUI
Anybody know how to avoid this error, thanks for replying .

In your code, you give the DataFrame x to the fit function. If you check closely, the first zipped item in the loop for xi, target in zip(X, y): is the column names, not the first data item. You can check that by printing xi in the loop.
So what you want to do is convert your data to an array beforehand:
x=np.array(df.iloc[0:100 , [0,2]])
or alternatively what you did for the targets y as well, use only the values of the DataFrame:
x=df.iloc[0:100 , [0,2]].values

Related

how to fix nomalization problem or overfitting

i tried to normalize my data sets column with this code , but the results on the column in (daddr)was not in 0 , 1 range enter image description here
and also the results in loss apear like following enter image description here
this is the code i used enter image description here
please tell me what is the missing thing to solve the (loss ) problem , how i could do the MinMax Normalization on all data sets column , is the problem overfitting or what ?
Normalizing the data is not always necessary. It depends with the model you use. Most of the time Normalizing is necessary when working with sigmoid or tanh function in your model. Do you really need to normalize the data ? Try without Normalizing.

How do I use numpy vectors as input for Trax?

I want to be able to use random numpy vectors as input for the Trax machine learning library. I find it helpful to be able to define my own simple inputs and outputs to see how the models are working. Am I doing this the right way, or am I missing something obvious and using the library totally wrong?
Colab notebook with working example
Make X matrix (Nx3) for inputs.
Make Y vector (Nx1) equal to the dot product between X and (1,0,0).
So, this makes y_i simply indicate which side of the yz plane the vector is on. The model would be learning this boundary.
def generate_xy(n):
X = np.random.normal(loc=0, scale=1, size=(n,3))
Y = np.dot(X, [1,0,0]) > 0
Y = Y.astype(int).reshape(-1,1)
return X, Y
X, Y = generate_xy(10000)
I reviewed the codebase at github and it looks like the input is supposed to be:
labeled_data: Iterator of batches of labeled data tuples. Each tuple
has
1+ data (input value) tensors followed by 1 label (target value)
tensor. All tensors are NumPy ndarrays or their JAX counterparts.
Create a very simple model:
model = tl.Serial(
tl.Dense(1),
tl.Sigmoid()
)
Define a train task: this is the part where I'm wondering if I'm doing something very wrong :)
train_task = ts.TrainTask(
labeled_data= zip(X,Y),
loss_layer=tl.CategoryCrossEntropy(),
optimizer=trax.optimizers.Adam(0.01),
n_steps_per_checkpoint=None
)
Define training loop and train model
training_loop = trax.supervised.training.Loop(model, train_task)
training_loop.run(9999) #one epoch
I'm not sure if this whole example is just contrived and way outside of what the library is intended to be used for, or if I'm just struggling to figure out the best way to handle inputs. Just looking for some guidance on best practices here. Thanks so much!

Unable to inverse_transform the value of feature because of different dimensionality

I'm designing a multivariate time series model. For that I'm inputing 5 features to lstm model and try to predict the output of 1 variable(i.e. whose value is dependent on itself and other 4 features).
For that I'm doing the feature scaling as follows:-
#Features Scaling
`from sklearn.preprocessing import MinMaxScaler
sc = MinMaxScaler(feature_range = (0,1))
training_set_scaled = sc.fit_transform(training_set)
print(training set scaled)`
Output:-
At the output of the model, I got the predicted value as:
However, when it tried to inverse transform it as:
predicted_stock_price = sc.inverse_transform(predicted_stock_price)
I got the the following error:-
non-broadcastable output operand with shape (65,1) doesn't match the broadcast shape (65,5)
Please help. Thank you in advance :)
The problem is that you use sc to min-max-scale the five features. Therefore, sc can also only be used to inverse transform the scaled version of the features (shown by you as output), which would give you back the original feature values.
The label (model output) is independent from that. You can also, but do not necessarily have to scale your dependent variable, and certainly not with the same scaler object.

Reshaping numpy array as an input to CNN

I have seen multiple posts on reshaping numpy arrays as inputs to CNN's however, I haven't been able to successfully reshape my array as an input to my CNN!
I have a CNN that merges with another model further downstream. The input shape of the CNN is (4,4,1) -- it is bigger but i have purposefully made it smaller to establish he pipeline and get it running before i put in the proper size.
the format will be the same however, its a 1 channel n x n np.array. I am getting errors when reshaping which I will mention after the code. The input dimensions are put in to the model as follows:
cnn_branch_input = tf.keras.layers.Input(shape=(4,4,1))
cnn_branch_two = tf.keras.layers.Conv2D(etc....)(cnn_branch_input)
the np array (which is originally a pandas dataframe) characteristics and reshaping are as follows:
np.array(array).shape
(4,4)
input = np.array(array).reshape(-1,1,4,4)
input.shape
(1,1,4,4)
the input to my merged model is as follows:
model.fit([cnn_input,gnn_input, gnn_node_feat], y,
#sample_weight=train_mask,
#validation_data=validation_data,
batch_size=4,
shuffle=False)
this causes an error which makes sense to me:
ValueError: Data cardinality is ambiguous:
x sizes: 1, 4, 4 -- Please provide data which shares the same first dimension.
So now when reshaping to intentionally have a 4x4 plus 1 channel shape as follows:
input = np.array(array).reshape(-1,4,4,1)
input.shape
(1,4,4,1)
Two things, the array reshapes to 4, 1x1 arrays, so it seems the structure of the original array is lost, and I get the same error!!
Notice that in both reshape methods, the shape is either (1,4,4,1) or (1,1,4,4).. the -1 entry simply becomes a 1, making the CNN think the first element is shape 1. I thought the -1 would allow me to successfully add the sample dimension as 'any number of samples'.
Simply entering the original (4,4) array, I receive the error that the CNN received a 2 dim array while a 4 dimension array is required.
Im really confused as to how to correctly reshape this array! I would appreciate any help!

Get label prediction from Cifar-10 model

I'm currently working on the Cifar-10 tutorial of tensorflow. I'd like to change the evaluation such that I can see for each image what the prediction of my model was, and whether it was true/false. I struggle with the first part: if I print the predictions (sess.run([top_k_op])) I get true/false values which I assume are whether the prediction was correct or not. However, if I try to print the actual prediction (I tried so far to print the logits, and print the top_k_op tensor), I get some numbers or values, but nothing that looks like the labels. What do I have to change about my code to actually see the labels that my model predicted?
You want to evaluate first the logits. This is a probability distribution over your classes out of your network. The index of the tensor with the higher value will give you the most likely class for your label.
you can use tf.argmax to get the index and then use the index in your labels to print it out
print labels[index]
You can figure out an answer by looking here
In svhn.py, at line 116 the predicted label is printed: print (step, int(test_labels[0]))
I did it in a clear way by using:
classification = sess.run(top_k_predict_op)
print (step, int(test_labels[0]))
print "network predicted:", classification[0], "for real label:", test_labels
Be sure that you are predicting on 24*24 images, in case you trained your model with the original version of the TensorFlow CIFAR-10 model.

Resources