im experimenting with Swift for TensorFlow to do some image segmentations, lets first look at some code:
let (loss, grad) = model.valueWithGradient { (model: UNet) -> Tensor<Float> in
let logits = model(batch.images)
print(logits.shape)
print(batch.corners.shape)
return softmaxCrossEntropy(logits: logits, probabilities: batch.corners)
}
Each batch contains some tensors of images of tickets, and some tensors of images of the corners, hence the two references: batch.images and batch.corners
You will see, that I also print their shapes, which both come out to be: [32, 324, 324, 4]
32 being the batch size, 324*324 being the size of the images, and 4 channels for each image.
The goal is to extract the position of the corners on the images.
I wanna use softmaxCrossEntropy as the loss function, but it gives be the following error:
Fatal error: logits and labels must be either 2-dimensional, or broadcasted to be 2-dimensional: file /swift-base/tensorflow-swift-apis/Sources/TensorFlow/Bindings/EagerExecution.swift, line 300
Current stack trace:
0 libswiftCore.so 0x00007f51ab60b940 swift_reportError + 50
1 libswiftCore.so 0x00007f51ab67ccf0 _swift_stdlib_reportFatalErrorInFile + 115
2 libswiftCore.so 0x00007f51ab5a6b48 <unavailable> + 3722056
3 libswiftCore.so 0x00007f51ab5a6cd7 <unavailable> + 3722455
4 libswiftCore.so 0x00007f51ab3794e8 <unavailable> + 1438952
5 libswiftCore.so 0x00007f51ab57a5ce <unavailable> + 3540430
6 libswiftCore.so 0x00007f51ab378c09 <unavailable> + 1436681
7 libswiftTensorFlow.so 0x00007f51a79b5f50 <unavailable> + 2899792
8 libswiftTensorFlow.so 0x00007f51a7809d10 checkOk(_:file:line:) + 434
9 libswiftTensorFlow.so 0x00007f51a7810ce0 TFE_Op.evaluateUnsafe() + 506
10 libswiftTensorFlow.so 0x00007f51a7811550 TFE_Op.execute<A, B>(_:_:) + 323
11 libswiftTensorFlow.so 0x00007f51a781a0c2 <unavailable> + 1212610
12 libswiftTensorFlow.so 0x00007f51a792fee0 static Raw.softmaxCrossEntropyWithLogits<A>(features:labels:) + 821
13 libswiftTensorFlow.so 0x00007f51a7a6f5b0 _vjpSoftmaxCrossEntropyHelper<A>(logits:probabilities:) + 84
14 libswiftTensorFlow.so 0x00007f51a7a6f6b0 AD__$s10TensorFlow25softmaxCrossEntropyHelper6logits13probabilitiesAA0A0VyxGAG_AGtAA0aB13FloatingPointRzlF__vjp_src_0_wrt_0 + 9
15 libswiftTensorFlow.so 0x00007f51a7ac9e10 AD__$s10TensorFlow19softmaxCrossEntropy6logits13probabilities9reductionAA0A0VyxGAH_A3HXFtAA0aB13FloatingPointRzlF__vjp_src_0_wrt_0 + 444
16 libswiftTensorFlow.so 0x00007f51a7b48f64 <unavailable> + 4550500
17 libswiftTensorFlow.so 0x00007f51a7ac9a60 AD__$s10TensorFlow19softmaxCrossEntropy6logits13probabilitiesAA0A0VyxGAG_AGtAA0aB13FloatingPointRzlF__vjp_src_0_wrt_0 + 616
Current stack trace:
frame #14: 0x00007f516f9a3c45 $__lldb_expr162`AD__$s15__lldb_expr_16110TensorFlow0C0VySfG02__a1_B4_1354UNetVcfU___vjp_src_0_wrt_0(model=<unavailable>, batch=<unavailable>) at <Cell 25>:17
frame #21: 0x00007f516f99b941 $__lldb_expr162`main at <Cell 25>:13:34
I understand that the inputs needs to be 2D, but I dont know how to handle that. I cant help but notice, that the Python version of the same function has a parameter alongAxis and wonder what I would do in S4TF to set a specific axis.
Hey #magnuskahr I can suggest you get in touch at /tensorflow/swift-apis under
Issues and the development team can look at it as soon as they can.
Also, I searched through closed and open Issues for softmaxCrossEntropy and came across a discussion here under /swift-apis/issues/422 which you may find interesting.
You can also post your questions in the official Swift for TensorFlow Google Group here.
Related
for a=1:50; %numbers 1 through 50
for b=1:50;
c=sqrt(a^2+b^2);
if c<=50&c(rem(c,1)==0);%if display only if c<=50 and c=c/1 has remainder of 0
pyth=[a,b,c];%pythagorean matrix
disp(pyth)
else c(rem(c,1)~=0);%if remainder doesn't equal to 0, omit output
end
end
end
answer=
3 4 5
4 3 5
5 12 13
6 8 10
7 24 25
8 6 10
8 15 17
9 12 15
9 40 41
10 24 26
12 5 13
12 9 15
12 16 20
12 35 37
14 48 50
15 8 17
15 20 25
15 36 39
16 12 20
16 30 34
18 24 30
20 15 25
20 21 29
21 20 29
21 28 35
24 7 25
24 10 26
24 18 30
24 32 40
27 36 45
28 21 35
30 16 34
30 40 50
32 24 40
35 12 37
36 15 39
36 27 45
40 9 41
40 30 50
48 14 50
This problem involves the Pythagorean theorem but we cannot use the built in function so I had to write one myself. The problem is for example columns 1 & 2 from the first two rows have the same numbers. How do I code it so it only deletes one of the rows if the columns 1 and 2 have the same number combination? I've tried unique function but it doesn't really delete the combinations. I have read about deleting duplicates from previous posts but those have confused me even more. Any help on how to go about this problem will help me immensely!
Thank you
welcome to StackOverflow.
The problem in your code seems to be, that pyth only contains 3 values, [a, b, c]. The unique() funcion used in the next line has no effect in that case, because only one row is contained in pyth. another issue is, that the values idx and out are calculated in each loop cycle. This should be placed after the loops. An example code could look like this:
pyth = zeros(0,3);
for a=1:50
for b=1:50
c = sqrt(a^2 + b^2);
if c<=50 && rem(c,1)==0
abc_sorted = sort([a,b,c]);
pyth = [pyth; abc_sorted];
end
end
end
% do final sorting outside of the loop
[~,idx] = unique(pyth, 'rows', 'stable');
out = pyth(idx,:);
disp(out)
a few other tips for writing MATLAB code:
You do not need to end for or if/else stements with a semicolon
else statements cover any other case not included before, so they do not need a condition.
Some performance reommendations:
Due to the symmetry of a and b (a^2 + b^2 = b^2 + a^2) the b loop could be constrained to for b=1:a, which would roughly save you half of the loop cycles.
if you use && for contencation of scalar values, the second part is not evaluated, if the first part already fails (source).
Regards,
Chris
You can also linearize your algorithm (but we're still using bruteforce):
[X,Y] = meshgrid(1:50,1:50); %generate all the combination
C = (X(:).^2+Y(:).^2).^0.5; %sums of two square for every combination
ind = find(rem(C,1)==0 & C<=50); %get the index
res = unique([sort([X(ind),Y(ind)],2),C(ind)],'rows'); %check for uniqueness
Now you could really optimized your algorithm using math, you should read this question. It will be useful if n>>50.
Example 1)
I have the code below
5#10+1*2
that generates
index value
0 12
1 12
2 12
3 12
4 12
How can I replace the number "1" by the index?
then generating
5#10+index*2
index value
0 10
1 12
2 14
3 16
4 18
update Example 2)
Now, if I have, let's say
mult:5;
t:select from ([]numC:1 3 6 4 1;[]s:50 16 53 6 33);
update lst:(numC#'s) from t
the last update will generate
numC s lst
1 50 50
3 16 16 16 16
6 53 53 53 53 53 53 53
4 6 6 6 6 6
1 33 33
How can I generate the "lst" column as per below?
numC s lst
1 50 50+0*mult
3 16 16+0*mult 16+1*mult 16+2*mult
6 53 53+0*mult 53+1*mult 53+2*mult 53+3*mult 53+4*mult 53+5*mult
4 6 6+0*mult 6+1*mult 6+2*mult 6+3*mult
1 33 33+0*mult
I tried something like
update lst:(numC#'s + (til numC)*mult) from t
but I am getting an error
ERROR: 'type
Thanks vm
Is this what you're looking for:
q)x:5
q)x#10+(til x)*2
10 12 14 16 18
http://code.kx.com/q/ref/arith-integer/#til
You can remove take # and use til to simplify to:
q)10+2*til 5
10 12 14 16 18
Using til will create a list of a list of 5 elements (0->4), so you will not need take 5 elements from the resulting list. Take will only be required if your list of indices is greater than 5.
Update:
For your second example the following should work:
q)update lst:{y+x*til z}'[mult;s;numC] from t
q)update lst:s+mult*til each numC from t
numC s lst
-------------------------
1 50 ,50
3 16 16 21 26
6 53 53 58 63 68 73 78
4 6 6 11 16 21
1 33 ,33
There are many ways with which we can get achieve this:
1) 10+2*til 5
2) (2*til 5) + 10
/ take operator: The dyadic take function creates lists. The left argument specifies the count and shape and the right argument provides the data.
It is useful for selecting from the front or end of a list.
https://code.kx.com/wiki/Reference/NumberSign
q)5#0 1 2 3 4 5 6 7 8 / take the first 5 items
0 1 2 3 4
q)-5#0 1 2 3 4 5 6 7 8 / take the last 5 elements
4 5 6 7 8
use take operator # only when it is required.
say we have 10 elements, of which we need five on output, then we can use:
5#10+2*til 10
/ The til function takes a non-negative integer argument X and returns the first X integers
Hi All Fortran Lovers,
I am trying to write to a file which outputs three variables as
program main
integer N, u
parameter(u=20)
open (u, FILE='points.dat', STATUS='new')
do 10 i= 1, 100
write(u,100) i, i*2, i*5
10 continue
100 format (I5, I10, 9X, I10)
close(u)
print *,'COMPLETE!!'
end
Which Gives output (points.dat stripped file content):
1 2 5
2 4 10
3 6 15
4 8 20
5 10 25
6 12 30
7 14 35
8 16 40
9 18 45
10 20 50
11 22 55
12 24 60
...
...
...
...
...
99 198 495
100 200 500
|(This line added by the write statement)
But I want something like this:
1 2 5
2 4 10
3 6 15
4 8 20
5 10 25
6 12 30
7 14 35
8 16 40
9 18 45
10 20 50
11 22 55
12 24 60
...
...
...
...
...
99 198 495
100 200 500|(The cursor stop here)
i.e. No space at start of each line. The last line stops after printing '500'
I tried using Horizontal spacing using '1X' specifier but no success.
Add advance='no' in write statement. If the line is not the last one, write EOL:
do 10 i= 1, 100
write(u,100,advance='no') i, i*2, i*5
if (i.ne.100) write(u,*)
10 continue
Edit: I see it now, it seems that the fortran program will add EOL to the end of file anyway. Then you have to use external programs to truncate your file, see for example https://www.quora.com/How-do-I-chop-off-just-the-last-byte-of-a-file-in-Bash .
It's hard to tell how it happens. I use mongodb for hadoop output (com.mongodb.hadoop). It seems all right in the beginning. When I tried to do a modification on the data, this issue just burst out.I cannot read the output collection data from both umongo(https://github.com/agirbal/umongo) & mongoHub(http://mongohub.todayclose.com/). I even cannot solve it by roll back my code.
Here is the trace from umongo
java.lang.UnsupportedOperationException: BSONDecoder doesn't understand type : 53 name: S
at org.bson.BasicBSONDecoder.decodeElement(BasicBSONDecoder.java:226)
at org.bson.BasicBSONDecoder.decodeElement(BasicBSONDecoder.java:206)
at org.bson.BasicBSONDecoder._decode(BasicBSONDecoder.java:79)
at org.bson.BasicBSONDecoder.decode(BasicBSONDecoder.java:57)
at com.mongodb.DefaultDBDecoder.decode(DefaultDBDecoder.java:56)
at com.mongodb.Response.<init>(Response.java:66)
at com.mongodb.DBPort.go(DBPort.java:128)
at com.mongodb.DBPort.call(DBPort.java:79)
at com.mongodb.DBTCPConnector.call(DBTCPConnector.java:218)
at com.mongodb.DBApiLayer$MyCollection.__find(DBApiLayer.java:305)
at com.mongodb.DBCursor._check(DBCursor.java:369)
at com.mongodb.DBCursor.getServerAddress(DBCursor.java:687)
at com.mongodb.DBCursor.toString(DBCursor.java:740)
at com.edgytech.umongo.CollectionPanel$4.getRoot(CollectionPanel.java:427)
at com.edgytech.umongo.DbJob.wrapUp(DbJob.java:130)
at com.edgytech.umongo.DbJob$1.done(DbJob.java:81)
at javax.swing.SwingWorker$5.run(SwingWorker.java:717)
at javax.swing.SwingWorker$DoSubmitAccumulativeRunnable.run(SwingWorker.java:814)
at sun.swing.AccumulativeRunnable.run(AccumulativeRunnable.java:95)
at javax.swing.SwingWorker$DoSubmitAccumulativeRunnable.actionPerformed(SwingWorker.java:824)
at javax.swing.Timer.fireActionPerformed(Timer.java:291)
at javax.swing.Timer$DoPostEvent.run(Timer.java:221)
at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:209)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:682)
at java.awt.EventQueue.access$000(EventQueue.java:85)
at java.awt.EventQueue$1.run(EventQueue.java:643)
at java.awt.EventQueue$1.run(EventQueue.java:641)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:87)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:652)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:296)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:211)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:201)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:196)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:188)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:122)
MongoDB: mongodb-2.2.0-x86_64 from HomeBrew(http://mxcl.github.com/homebrew/)
OS: Mac OS 10.8.1
Driver: 2.7.2 & 2.7.3
UPDATE
The error from mongo shell
Wed Sep 26 09:24:20 Assertion: 10320:BSONElement: bad type 53
0x10004656b 0x1000b9b1e 0x100013a62 0x100292425 0x10011ef2e 0x100120ec8 0x10014ab74 0x1001498f4 0x1001cd414 0x1001490fc 0x10014ad81 0x100159c8f 0x100255418 0x10025551b 0x10029d0ba 0x100007277 0x10000919a 0x100000dd4 0x1
0 mongo 0x000000010004656b _ZN5mongo15printStackTraceERSo + 43
1 mongo 0x00000001000b9b1e _ZN5mongo11msgassertedEiPKc + 206
2 mongo 0x0000000100013a62 _ZNK5mongo11BSONElement4sizeEv + 418
3 mongo 0x0000000100292425 _ZN5mongo16resolveBSONFieldEP9JSContextP8JSObjectljPS3_ + 389
4 mongo 0x000000010011ef2e js_LookupPropertyWithFlags + 734
5 mongo 0x0000000100120ec8 js_GetProperty + 136
6 mongo 0x000000010014ab74 js_Interpret + 3396
7 mongo 0x00000001001498f4 js_Invoke + 3300
8 mongo 0x00000001001cd414 fun_apply + 532
9 mongo 0x00000001001490fc js_Invoke + 1260
10 mongo 0x000000010014ad81 js_Interpret + 3921
11 mongo 0x0000000100159c8f js_Execute + 1103
12 mongo 0x0000000100255418 JS_EvaluateUCScriptForPrincipals + 168
13 mongo 0x000000010025551b JS_EvaluateScript + 107
14 mongo 0x000000010029d0ba _ZN5mongo7SMScope4execERKNS_10StringDataERKSsbbbi + 330
15 mongo 0x0000000100007277 _Z5_mainiPPc + 12599
16 mongo 0x000000010000919a main + 42
17 mongo 0x0000000100000dd4 start + 52
18 ??? 0x0000000000000001 0x0 + 1
Error: BSONElement: bad type 53
I have a function f(x,y) which has certain symmetries that I would like to plot. Here is an example:
This plot can be generated with:
[x,y,z] =
0 0 0.1415
0.1999 0.1999 0.1165
0.2760 0 0.1268
0.3694 0.3694 0.0983
0.4830 0 0.1142
0.5090 0.5090 0.0903
0.5550 0.1871 0.0881
0.6189 0.3558 0.0715
0.6197 0.6197 0.0907
0.6399 0 0.1056
0.7071 0.7071 0.1415
0.7169 0.4835 0.0869
0.7215 0.1200 0.0859
0.7304 0.2392 0.0680
0.7643 0 0.1005
0.7926 0.3574 0.0856
0.8090 0.5878 0.1393
0.8581 0.1122 0.0821
0.8634 0.2343 0.0878
0.8794 0 0.0986
0.8910 0.4540 0.1332
0.9511 0.3090 0.1253
0.9877 0.1564 0.1191
1.0000 0 0.1169
t =
6 4 8
12 6 8
8 4 7
4 2 7
8 7 14
14 7 13
3 2 1
5 7 3
3 7 2
17 12 21
6 12 9
9 17 11
12 17 9
10 15 13
10 7 5
13 7 10
21 12 16
16 12 8
8 14 16
18 14 13
15 20 18
13 15 18
24 23 18
18 20 24
21 16 22
23 22 19
19 18 23
14 18 19
19 16 14
19 22 16
trisurf(t,x,y,z)
So I know that function has a reflection symmetry about y=x and then the resulting function is to be repeated in all the quadrants. Here is the code to do this:
allx = [x; x;-x;-x;y; y;-y;-y];
ally = [y;-y; y;-y;x;-x; x;-x];
allz = [z; z; z; z;z; z; z; z];
These are the new vertices for the surface I want to plot. Now how do I properly generate the faces for this new surface?
When I use a finer mesh and add some pretty lights it should look something like this:
Speculative:
So your question is about how to set-up the first argument of trisurf, i.e. how to define the extended t in your code. According to the docs this is the index into the vertices defined by the remaining arguments. I don't have MATLAB installed on this machine, but what happens if you do:
allx = [x; x;-x;-x];
ally = [y;-y; y;-y];
allz = [z; z; z; z];
s = size(x,1);
t = [t; t + s; t + 2*s; t + 3*s]
Just trying to think if this makes sense and if/how it extends into the other quadrants.