I'm trying to build a neural network that can answer to the xor problem. My code is the following:
using MXNet
using Distributions
using PyPlot
xor_data = zeros(4,2)
xor_data[1:0] = 1
xor_data[1:1] = 1
xor_data[2:0] = 1
xor_data[2:1] = 0
xor_data[3:0] = 0
xor_data[3:1] = 1
xor_data[4:0] = 0
xor_data[4:1] = 0
xor_labels = zeros(4)
xor_labels[1] = 0
xor_labels[2] = 1
xor_labels[3] = 1
xor_labels[4] = 0
batchsize = 4
trainprovider = mx.ArrayDataProvider(:data => xor_data, batch_size=batchsize, shuffle=true, :label => xor_labels)
evalprovider = mx.ArrayDataProvider(:data => xor_data, batch_size=batchsize, shuffle=true, :label => xor_labels)
data = mx.Variable(:data)
label = mx.Variable(:label)
net = #mx.chain mx.Variable(:data) =>
mx.FullyConnected(num_hidden=2) =>
mx.Activation(act_type=:relu) =>
mx.FullyConnected(num_hidden=2) =>
mx.Activation(act_type=:relu) =>
mx.FullyConnected(num_hidden=1) =>
mx.Activation(act_type=:relu) =>
model = mx.FeedForward(net, context=mx.cpu())
optimizer = mx.SGD(lr=0.01, momentum=0.9, weight_decay=0.00001)
initializer = mx.NormalInitializer(0.0,0.1)
eval_metric = mx.MSE()
mx.fit(model, optimizer, initializer, eval_metric, trainprovider, eval_data = evalprovider, n_epoch = 100)
mx.fit(model, optimizer, eval_metric, trainprovider, eval_data = evalprovider, n_epoch = 100)
But I'm getting the following error:
LoadError: AssertionError: Number of samples in label is mismatch
with data
in expression starting on line 22 in #ArrayDataProvider#6428(::Int64,
::Bool, ::Int64, ::Int64, ::Type{T}, ::Pair{Symbol,Array{Float64,2}},
::Pair{Symbol,Array{Float64,1}}) at io.jl:324 in
(::Core.#kw#Type)(::Array{Any,1}, ::Type{MXNet.mx.ArrayDataProvider},
::Pair{Symbol,Array{Float64,2}}, ::Pair{Symbol,Array{Float64,1}}) at
:0 in include_string(::String, ::String) at loading.jl:441
in include_string(::String, ::String) at sys.dylib:? in
include_string(::Module, ::String, ::String) at eval.jl:32 in
(::Atom.##59#62{String,String})() at eval.jl:81 in
withpath(::Atom.##59#62{String,String}, ::String) at utils.jl:30 in
withpath(::Function, ::String) at eval.jl:46 in macro expansion at
eval.jl:79 [inlined] in (::Atom.##58#61{Dict{String,Any}})() at
task.jl:60
I want to feed to the network to values (0 or 1) and get a single value. Were is my error?
Dimensions of xor_data are wrong, it should have 4 columns, not 4 rows (and, by the way, you're not initialising it the way you think you do, since arrays in Julia are indexed from 1, not from 0).
Look:
julia> xor_data = [ [1. 1]; [0 1]; [1 0]; [0 0] ]
4×2 Array{Float64,2}:
1.0 1.0
0.0 1.0
1.0 0.0
0.0 0.0
julia> xor_labels
4-element Array{Float64,1}:
0.0
1.0
1.0
0.0
julia> mx.ArrayDataProvider(:data => xor_data, :labels => xor_labels)
ERROR: AssertionError: Number of samples in labels is mismatch with data
in #ArrayDataProvider#6428(::Int64, ::Bool, ::Int64, ::Int64, ::Type{T}, ::Pair{Symbol,Array{Float64,2}}, ::Pair{Symbol,Array{Float64,1}}) at /Users/alexey/.julia/v0.5/MXNet/src/io.jl:324
in MXNet.mx.ArrayDataProvider(::Pair{Symbol,Array{Float64,2}}, ::Pair{Symbol,Array{Float64,1}}) at /Users/alexey/.julia/v0.5/MXNet/src/io.jl:280
julia> xor_data = [ [1. 0 1 0]; [1 1 0 0] ]
2×4 Array{Float64,2}:
1.0 0.0 1.0 0.0
1.0 1.0 0.0 0.0
julia> mx.ArrayDataProvider(:data => xor_data, :labels => xor_labels)
MXNet.mx.ArrayDataProvider(Array{Float32,N}[
Float32[1.0 0.0 1.0 0.0; 1.0 1.0 0.0 0.0]],Symbol[:data],Array{Float32,N}[
Float32[0.0 1.0 1.0 0.0]],Symbol[:labels],4,4,false,0.0f0,0.0f0,MXNet.mx.NDArray[mx.NDArray{Float32}(2,4)],MXNet.mx.NDArray[mx.NDArray{Float32}(4,)])
Related
demorgans law states in this article
https://ryanstutorials.net/boolean-algebra-tutorial/boolean-algebra-laws.php#introduction
that NOT(P & K) = NOT(P) OR NOT(K)
so i did a little testing with 1's and zeros
NOT(1 AND 1) = false
NOT(1 AND 0) = false
NOT(0 AND 1) = false
NOT(O AND 0) = true
so then i did the second expression
NOT(1) OR NOT(O) = TRUE
NOT(0) OR NOT(1) = TRUE
NOT(0) OR NOT(0) = TRUE
NOT(1) OR NOT(1) = FALSE
what am i doing wrong here?
You have to do this table :
P K | Q Q'
0 0 | 0 1
0 1 | 0 1
1 0 | 0 1
1 1 | 1 0
The Q is the Output and the Q' is the Output', 0=false and 1=true;
So, the first column is obtained doing P*K and the second denying Qso P'+K'.
I want to write a program that can find the N-th number,which only contains factor 2 , 3 or 5.
def method3(n:Int):Int = {
var q2 = mutable.Queue[Int](2)
var q3 = mutable.Queue[Int](3)
var q5 = mutable.Queue[Int](5)
var count = 1
var x:Int = 0
while(count != n){
val minVal = Seq(q2,q3,q5).map(_.head).min
if(minVal == q2.head){
x = q2.dequeue()
q2.enqueue(2*x)
q3.enqueue(3*x)
q5.enqueue(5*x)
}else if(minVal == q3.head){
x = q3.dequeue()
q3.enqueue(3*x)
q5.enqueue(5*x)
}else{
x = q5.dequeue()
q5.enqueue(5*x)
}
count+=1
}
return x
}
println(method3(1000))
println(method3(10000))
println(method3(100000))
The results
51200000
0
0
When the input number gets larger , I get 0 from the function.
But if I change the function to
def method3(n:Int):Int = {
...
q5.enqueue(5*x)
}
if(x > 1000000000) println(('-',x)) //note here!!!
count+=1
}
return x
}
The results
51200000
(-,1006632960)
(-,1007769600)
(-,1012500000)
(-,1019215872)
(-,1020366720)
(-,1024000000)
(-,1025156250)
(-,1033121304)
(-,1036800000)
(-,1048576000)
(-,1049760000)
(-,1054687500)
(-,1061683200)
(-,1062882000)
(-,1073741824)
0
.....
So I don't know why the result equals to 0 when the input number grows larger.
An Int is only 32 bits (4 bytes). You're hitting the limits of what an Int can hold.
Take that last number you encounter: 1073741824. Multiply that by 2 and the result is negative (-2147483648). Multiply it by 4 and the result is zero.
BTW, if you're working with numbers "which only contains factor 2, 3 or 5", in other words the numbers 2, 3, 4, 5, 6, 8, 9, 10, 12, 14, 15, ... etc., then the 1,000th number in that sequence shouldn't be that big. By my calculations the result should only be 1365.
I didn't understood the concept of the For-In loop in swift 3 , can anyone explain to us it m thanks in advance
var total = 0
for i in 0..<4 {
total += i
}
print(total)
The result of total is 6 , Why ?
i=0 =>
total = 0+0 =0
i=1 =>
total = 0+1 = 1
i=2 =>
total = 1+2 = 3
i=3 =>
total = 3+3 =6
it's simply alogrithm ;-)
i never reach 4 because you said it STRICTLY inferior to 4 =)
(Do I answer your question?)
Your loop will be vary from 0 to 3 i.e. 0,1,2,3 but if you want it will vary from 0 to 4 then try this -
var total = 0
for i in 0...4 {
total += i
}
print(total)
I've spent the whole day trying to figure out how to JOIN / group my query below, can someone help me or guide me to complete the desired output? -i enclosed some of the foreign key values for easy reference)
Below is my Table Grade
rowID testID studentID Grade
1 1 1(class2011) 50
2 1 1(class2011) 90
3 2 1(class2011) 100
4 2 2(class2012) 85
--Student table
StudentID Classyear
1 2(class2011)
2 3(class2012)
3 1(class2010)
--Classyear Table
ClassYearID Desc
1 2010
2 2011
3 2012
My query below (to display the testID, failed(passing rate=80), passed, taken, Rate)
var h = list.GroupBy(a => a.testID)
.Select(a => {
int _failed = a.Count(g => g.Grade < 80);
int _passed = a.Count(g => g.Grade >= 80);
int _rate = (int)(_passed / (double)a.Count() * 100.0);
return new {
testID = a.Key,
failed = _failed,
passed = _passed,
taken = a.Count(),
rate = _rate,
};
});
The result1
testID failed passed taken Rate
1 1 1 2 50%
2 0 2 2 100%
Now here's my BIG problem: the students in my table grade above DOES NOT belong in the
same classyear. I need to group this by testID and classyear.
See below for the desired output:
testID ClassyearID failed passed taken Rate
1 1(2010) 0 0 0 0
1 2(2011) 1 1 2 50%
1 3(2012) 0 0 0 0
2 1(2010) 0 0 0 0
2 2(2011) 0 1 1 100%
2 3(2012) 0 1 1 100%
You can immediately see the difference from result 1. stats for Test2 is divided for class 2011 and class 2012.
Hope I did explain clear enough my intentions. Maybe I will post later my crappy code trying to get the desired output. Thanks
Can't you just do this?:
var h = list.GroupBy(a =>new {a.testID,a.classyear})
.Select(a => {
int _failed = a.Count(g => g.Grade < 80);
int _passed = a.Count(g => g.Grade >= 80);
int _rate = (int)(_passed / (double)a.Count() * 100.0);
return new {
testID = a.Key.testID,
classyear=a.Key.classyear,
failed = _failed,
passed = _passed,
taken = a.Count(),
rate = _rate,
};
});
I'm trying to read HDF5 files with Matlab. I created the files in Fortran, which is only relevant in that I used h5dsattach_scale_f to attached scale datasets to each dimension of my given primary dataset. Most of my logic works well, but I'm having trouble reading the attributes of my primary dataset in order to get at the attached scales.
I start by iterating through each dataset in the file. Once I know I have my primary dataset, I iterate through its attributes with this call:
[status, index_out, SD] = H5A.iterate(dset_id, 'H5_INDEX_NAME', 'H5_ITER_NATIVE', 0, #hdf5_sds_attr_iter, SD);
That calls this function for every attribute:
function [status, SD] = hdf5_sds_attr_iter(dset_id, attr_name, info, SD)
status = 0;
disp(attr_name);
if ~strcmp(attr_name, 'DIMENSION_LIST')
return;
end
attr_id = H5A.open(dset_id, attr_name, 'H5P_DEFAULT');
space = H5A.get_space (attr_id);
[~, dims, ~] = H5S.get_simple_extent_dims(space);
info2 = H5A.get_info(attr_id);
disp(info2);
rdata = H5A.read(attr_id, 'H5ML_DEFAULT');
disp(rdata);
for i = 1:dims
disp(rdata{i});
end
H5S.close(space);
H5A.close(attr_id);
end
This is the output:
DIMENSION_LIST
3
corder_valid: 1
corder: 0
cset: 0
data_size: 48
[8x1 uint8]
[8x1 uint8]
[8x1 uint8]
184
17
0
0
0
0
0
0
32
28
0
0
0
0
0
0
240
29
0
0
0
0
0
0
If I do h5dump on the dataset, this is what that attribute looks like:
ATTRIBUTE "DIMENSION_LIST" {
DATATYPE H5T_VLEN { H5T_REFERENCE { H5T_STD_REF_OBJECT }}
DATASPACE SIMPLE { ( 3 ) / ( 3 ) }
DATA {
(0): (DATASET 1400 /beamdata scale rank 1 ),
(1): (DATASET 6512 /beamdata scale rank 2 ),
(2): (DATASET 6976 /beamdata scale rank 3 )
}
}
Since those numbers (1400, 6512, 6976) do not appear elsewhere in the dump, I don't know how to use them or the output of H5A.read (rdata) to actually get at the scale data. The Matlab HDF5 documentation is rather silent on what to do with attribute data. Does anyone know how to process attribute reference data correctly?