Writing small numbers in Latex figures - latex

I have a basic figure where I want the following numbers to be in the Y axis
0, 0.01, 0.02, 0.03, 0.04, 0.05, 0.06, 0.07, 0.08, 0.09, 0.1
When I write them in the code they appear in a different way as you see in the screenshot.
I know this is mathematically correct but they look odd and I want to them to be shown exactly as I put them in the code.
this is the code
\documentclass{article}
\usepackage{graphicx} % Required for inserting images
\usepackage{pgfplots}
\begin{document}
\begin{figure}
\centering
\begin{tikzpicture} [style={outer sep=5}]
\begin{axis}[ymin=0,ymax=0.1, xmin=15, xmax=50,
xtick={15,20,...,50},
ytick={0,0.01,...,0.1},
grid={major},clip=false,grid style={dashed},
title={},
legend style={at={(1.2 ,1)},anchor=north},
font=\footnotesize]
% 25
\addplot[color=green, mark=*, samples=10] [error bars/.cd,y dir=both, y explicit] coordinates {
(15, 0.0032)
(20, 0.0039)
(25, 0.0052)
(30, 0.007)
(35, 0.009)
(40, 0.0095)
(45, 0.01)
(50, 0.012)
};
\end{axis}
\end{tikzpicture}
\caption[]{}
\end{figure}
\end{document}
The other figure code is
\begin{figure} [H]
\centering
\begin{tikzpicture} [style={outer sep=5}]
\begin{axis}[ymin=0.004,ymax=0.018, xmin=15, xmax=50,
xlabel= $Number of Nodes$, ylabel = $end-to-end (sec)$,
xtick={15,20,...,50},
ytick={0.004,0.005,...,0.018},
yticklabel style={/pgf/number format/fixed},
grid={major},clip=false,grid style={dashed},
title={},
legend style={at={(1.2 ,1)},anchor=north},
font=\footnotesize]
% TAODV
\addplot[color=blue, mark=*, samples=10] [error bars/.cd,y dir=both, y explicit] coordinates {
(15, 0.0072) +-(0.001, -0.001)
(20, 0.0079) +-(0.001, -0.001)
(25, 0.0092) +-(0.001, -0.001)
(30, 0.01) +-(0.001, -0.001)
(35, 0.013) +-(0.0018, -0.0018)
(40, 0.0137) +-(0.001, -0.001)
(45, 0.015) +-(0.001, -0.001)
(50, 0.0168)+-(0.001, -0.001)
};
% AODV
\addplot[color=red, mark=*, samples=10] [error bars/.cd,y dir=both, y explicit] coordinates {
(15, 0.0052) +-(0.001, -0.001)
(20, 0.0057) +-(0.001, -0.001)
(25, 0.0067) +-(0.001, -0.001)
(30, 0.0074) +-(0.001, -0.001)
(35, 0.0082) +-(0.0018, -0.0018)
(40, 0.0095) +-(0.001, -0.001)
(45, 0.01) +-(0.001, -0.001)
(50, 0.0109) +-(0.001, -0.001)
};
\legend{
$TAODV$,
$AODV$
}
\end{axis}
\end{tikzpicture}
\caption[End-to-end delay of TAODV vs. AODV]{End-to-end delay of TAODV vs. AODV with \%95 confidence interval}
\end{figure}

You can use yticklabel style={/pgf/number format/fixed} to change the number format:
\documentclass{article}
\usepackage{graphicx} % Required for inserting images
\usepackage{pgfplots}
\begin{document}
\begin{figure}
\centering
\begin{tikzpicture} [style={outer sep=5}]
\begin{axis}[ymin=0,ymax=0.1, xmin=15, xmax=50,
xtick={15,20,...,50},
ytick={0,0.01,...,0.1},
grid={major},clip=false,grid style={dashed},
title={},
legend style={at={(1.2 ,1)},anchor=north},
font=\footnotesize,
scaled y ticks=false,
yticklabel style={/pgf/number format/fixed},
]
% 25
\addplot[color=green, mark=*, samples=10] [error bars/.cd,y dir=both, y explicit] coordinates {
(15, 0.0032)
(20, 0.0039)
(25, 0.0052)
(30, 0.007)
(35, 0.009)
(40, 0.0095)
(45, 0.01)
(50, 0.012)
};
\end{axis}
\end{tikzpicture}
\caption[]{}
\end{figure}
\end{document}

Related

ValueError:The requested array has an inhomogeneous shape after 2 dimensions.The detected shape was(318, 20)+inhomogeneous part

from sklearn.neural_network import MLPClassifier
#Initialise Multi Layer Perceptron Classifier (MLP)
model = MLPClassifier(alpha = 0.01, batch_size = 256, epsilon = 1e-08, hidden_layer_sizes = (400,), learning_rate = 'adaptive', max_iter = 500)
model.fit(X_train, y_train)
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-41-d768f88d541e> in <module>
----> 1 model.fit(X_train, y_train)
5 frames
/usr/local/lib/python3.8/dist-packages/sklearn/neural_network/_multilayer_perceptron.py in fit(self, X, y)
750 Returns a trained MLP model.
751 """
--> 752 return self._fit(X, y, incremental=False)
753
754 def _check_solver(self):
/usr/local/lib/python3.8/dist-packages/sklearn/neural_network/_multilayer_perceptron.py in _fit(self, X, y, incremental)
391 )
392
--> 393 X, y = self._validate_input(X, y, incremental, reset=first_pass)
394
395 n_samples, n_features = X.shape
/usr/local/lib/python3.8/dist-packages/sklearn/neural_network/_multilayer_perceptron.py in _validate_input(self, X, y, incremental, reset)
1098
1099 def _validate_input(self, X, y, incremental, reset):
-> 1100 X, y = self._validate_data(
1101 X,
1102 y,
/usr/local/lib/python3.8/dist-packages/sklearn/base.py in _validate_data(self, X, y, reset, validate_separately, **check_params)
579 y = check_array(y, **check_y_params)
580 else:
--> 581 X, y = check_X_y(X, y, **check_params)
582 out = X, y
583
/usr/local/lib/python3.8/dist-packages/sklearn/utils/validation.py in check_X_y(X, y, accept_sparse, accept_large_sparse, dtype, order, copy, force_all_finite, ensure_2d, allow_nd, multi_output, ensure_min_samples, ensure_min_features, y_numeric, estimator)
962 raise ValueError("y cannot be None")
963
--> 964 X = check_array(
965 X,
966 accept_sparse=accept_sparse,
/usr/local/lib/python3.8/dist-packages/sklearn/utils/validation.py in check_array(array, accept_sparse, accept_large_sparse, dtype, order, copy, force_all_finite, ensure_2d, allow_nd, ensure_min_samples, ensure_min_features, estimator)
744 array = array.astype(dtype, casting="unsafe", copy=False)
745 else:
--> 746 array = np.asarray(array, order=order, dtype=dtype)
747 except ComplexWarning as complex_warning:
748 raise ValueError(
ValueError: setting an array element with a sequence. The requested array has an inhomogeneous shape after 2 dimensions. The detected shape was (318, 20) + inhomogeneous part.
for i in X_train:
print(i.shape)
(20, 100)
(20, 80)
(20, 69)
(20, 60)
(20, 67)
(20, 60)
(20, 60)
(20, 69)
(20, 63)
(20, 60)
(20, 92)
(20, 70)
(20, 63)
(20, 79)
(20, 53)
(20, 81)
(20, 86)
(20, 69)
(20, 87)
(20, 79)
(20, 76)
(20, 58)
(20, 84)
(20, 89)
(20, 90)
(20, 94)
(20, 117)
(20, 57)
(20, 83)
(20, 63)
(20, 81)
(20, 76)
(20, 84)
(20, 80)
(20, 73)
(20, 120)
(20, 79)
(20, 73)
(20, 81)
(20, 84)
(20, 81)
(20, 86)
(20, 74)
(20, 92)
(20, 84)
(20, 104)
(20, 73)
(20, 54)
(20, 81)
(20, 84)
(20, 76)
(20, 81)
(20, 107)
(20, 109)
(20, 107)
(20, 86)
(20, 94)
(20, 80)
(20, 73)
(20, 69)
(20, 107)
(20, 67)
(20, 79)
(20, 76)
(20, 70)
(20, 56)
(20, 70)
(20, 66)
(20, 71)
(20, 83)
(20, 74)
(20, 64)
(20, 76)
(20, 94)
(20, 69)
(20, 71)
(20, 103)
(20, 80)
(20, 97)
(20, 83)
(20, 79)
(20, 70)
(20, 70)
(20, 99)
(20, 90)
(20, 67)
(20, 64)
(20, 73)
(20, 87)
(20, 71)
(20, 84)
(20, 69)
(20, 84)
(20, 66)
(20, 92)
(20, 90)
(20, 67)
(20, 104)
(20, 71)
(20, 63)
(20, 96)
(20, 79)
(20, 84)
(20, 104)
(20, 54)
(20, 97)
(20, 81)
(20, 61)
(20, 79)
(20, 81)
(20, 79)
(20, 93)
(20, 102)
(20, 107)
(20, 76)
(20, 106)
(20, 94)
(20, 79)
(20, 99)
(20, 66)
(20, 100)
(20, 70)
(20, 83)
(20, 87)
(20, 93)
(20, 76)
(20, 77)
(20, 76)
(20, 73)
(20, 66)
(20, 107)
(20, 71)
(20, 79)
(20, 699)
(20, 57)
(20, 57)
(20, 70)
(20, 107)
(20, 84)
(20, 71)
(20, 71)
(20, 99)
(20, 67)
(20, 77)
(20, 92)
(20, 81)
(20, 76)
(20, 54)
(20, 77)
(20, 63)
(20, 64)
(20, 83)
(20, 66)
(20, 64)
(20, 110)
(20, 81)
(20, 74)
(20, 64)
(20, 76)
(20, 71)
(20, 71)
(20, 87)
(20, 146)
(20, 96)
(20, 97)
(20, 103)
(20, 70)
(20, 60)
(20, 61)
(20, 77)
(20, 70)
(20, 104)
(20, 83)
(20, 96)
(20, 53)
(20, 86)
(20, 64)
(20, 90)
(20, 92)
(20, 64)
(20, 84)
(20, 69)
(20, 63)
(20, 69)
(20, 46)
(20, 50)
(20, 56)
(20, 60)
(20, 100)
(20, 50)
(20, 51)
(20, 96)
(20, 92)
(20, 87)
(20, 84)
(20, 63)
(20, 64)
(20, 90)
(20, 71)
(20, 54)
(20, 126)
(20, 80)
(20, 79)
(20, 63)
(20, 89)
(20, 94)
(20, 77)
(20, 87)
(20, 69)
(20, 67)
(20, 90)
(20, 84)
(20, 117)
(20, 77)
(20, 70)
(20, 80)
(20, 90)
(20, 81)
(20, 81)
(20, 64)
(20, 79)
(20, 56)
(20, 60)
(20, 79)
(20, 73)
(20, 58)
(20, 67)
(20, 89)
(20, 80)
(20, 57)
(20, 96)
(20, 83)
(20, 70)
(20, 81)
(20, 69)
(20, 83)
(20, 80)
(20, 58)
(20, 93)
(20, 64)
(20, 63)
(20, 60)
(20, 64)
(20, 92)
(20, 63)
(20, 80)
(20, 106)
(20, 93)
(20, 63)
(20, 80)
(20, 96)
(20, 90)
(20, 112)
(20, 80)
(20, 90)
(20, 94)
(20, 86)
(20, 94)
(20, 79)
(20, 80)
(20, 76)
(20, 47)
(20, 60)
(20, 76)
(20, 90)
(20, 70)
(20, 96)
(20, 142)
(20, 92)
(20, 89)
(20, 84)
(20, 69)
(20, 71)
(20, 81)
(20, 106)
(20, 63)
(20, 80)
(20, 69)
(20, 86)
(20, 92)
(20, 69)
(20, 83)
(20, 80)
(20, 57)
(20, 61)
(20, 67)
(20, 97)
(20, 94)
(20, 94)
(20, 54)
(20, 76)
(20, 89)
(20, 70)
(20, 79)
(20, 69)
(20, 67)
(20, 53)
(20, 90)
(20, 81)
(20, 94)
(20, 100)
(20, 90)
(20, 70)
(20, 70)
(20, 71)
(20, 83)
(20, 70)
(20, 84)
(20, 86)
(20, 66)
(20, 87)
(20, 70)
(20, 63)
(20, 69)
(20, 94)
(20, 58)
(20, 92)
(20, 83)
need help to solve this error
In MLP, the input size of all the samples must be the same. But in your case, the sample sizes are different which is not permitted. So you can do two things:
Either truncate all the samples so that each sample has the size of the smallest sample.
Zero-pad each sample so that each sample has the size of the largest sample.

How to make an enumerating in a foreach command along with its positioning in TikZ?

I am using TikZ in LaTeX. I want to make a foreach to enumerate text in several position. Example;
Text 'Statement 1' in coordinate A;
Text 'Statement 2' in coordinate B;
Text 'Statement 3' in coordinate C;
Text 'Statement 4' in coordinate D;
Here is my code
\documentclass[12pt]{article}
\usepackage[utf8]{inputenc}
\usepackage{tikz}
\usetikzlibrary{intersections, angles, quotes}
\usepackage{pgffor}
\usepackage{amsfonts}
\usepackage{amsmath}
\usepackage{amssymb}
\begin{document}
\begin{center}
\begin{tikzpicture}
\draw[very thick] (7,5) rectangle (11,6.5);
\coordinate (A) at (9,5.75);
\node at (A) {IF-CONDITIONAL};
\draw[very thick, ->] (9,5)--(9,3);
\foreach \s in {3, -2.5, -8, -13.5} \draw[very thick]
(9,\s)--(6,{\s-2})--(9,{\s-4})--(12,{\s-2})--cycle;
\node at (9,1) {Condition 1 (\textit{true?})};
\node at (9,-4.5) {Condition 2 (\textit{true?})};
\node at (9,-10){Condition ... (\textit{true?})};
\node at (9,-15.5){Else (\textit{true?})};
\foreach \s in {-1, -6.5, -12} \draw[very thick, ->] (9,\s)--(9,{\s-1.5})
node[fill=white, midway]{NO};
\foreach \s in {1, -4.5, -10, -15.5} \draw[very thick, ->] (12,\s)--(14,\s)
node[fill=white, midway]{YES};
\foreach \s in {1, -4.5, -10, -15.5} \draw[very thick, ->] (19, \s)--(20, \s);
\foreach \x in {0,-5.5, -11, -16.5} \draw[very thick] (14,\x) rectangle (19,{\x+2});
\draw[very thick, ->] (9, -17.5)--(9,-19)--(20, -19);
\node[fill=white] at (9, -18.25){NO};
\draw[very thick, ->] (20, 1)--(20, -21);
\node at (16.5,1){Statement 1};
\end{tikzpicture}
\end{center}
\end{document}
This code produce the output
How to write the text 'Statement 1/2/3/4' in the blank boxes using foreach ?
To answer the question you ask: You can use the count option of \foreach
\documentclass[12pt]{article}
\usepackage[utf8]{inputenc}
\usepackage{tikz}
\usetikzlibrary{intersections, angles, quotes}
\usepackage{pgffor}
\usepackage{amsfonts}
\usepackage{amsmath}
\usepackage{amssymb}
\begin{document}
\begin{center}
\begin{tikzpicture}
\draw[very thick] (7,5) rectangle (11,6.5);
\coordinate (A) at (9,5.75);
\node at (A) {IF-CONDITIONAL};
\draw[very thick, ->] (9,5)--(9,3);
\foreach \s in {3, -2.5, -8, -13.5} \draw[very thick]
(9,\s)--(6,{\s-2})--(9,{\s-4})--(12,{\s-2})--cycle;
\node at (9,1) {Condition 1 (\textit{true?})};
\node at (9,-4.5) {Condition 2 (\textit{true?})};
\node at (9,-10){Condition ... (\textit{true?})};
\node at (9,-15.5){Else (\textit{true?})};
\foreach \s in {-1, -6.5, -12} \draw[very thick, ->] (9,\s)--(9,{\s-1.5})
node[fill=white, midway]{NO};
\foreach \s in {1, -4.5, -10, -15.5} \draw[very thick, ->] (12,\s)--(14,\s)
node[fill=white, midway]{YES};
\foreach \s in {1, -4.5, -10, -15.5} \draw[very thick, ->] (19, \s)--(20, \s);
\foreach[count=\xi] \x in {0,-5.5, -11, -16.5}{
\node[draw,very thick,minimum width=5cm,minimum height=2cm] at (16.5,\x+1) {Statement \xi};
}
\draw[very thick, ->] (9, -17.5)--(9,-19)--(20, -19);
\node[fill=white] at (9, -18.25){NO};
\draw[very thick, ->] (20, 1)--(20, -21);
\end{tikzpicture}
\end{center}
\end{document}
However instead of manually messing around with absolute coordinates, you could simply let tikz do the positioning for you:
\documentclass[12pt]{article}
\usepackage[utf8]{inputenc}
\usepackage{tikz}
\usetikzlibrary{intersections, angles, quotes,positioning,shapes.geometric}
\usepackage{pgffor}
\usepackage{amsfonts}
\usepackage{amsmath}
\usepackage{amssymb}
\begin{document}
\begin{center}
\begin{tikzpicture}[
scale=0.6,
transform shape,
very thick,
node distance=1.6cm and 3cm,
dia/.style={draw,diamond,shape aspect=1.3,minimum width=6cm,minimum height=4cm}
]
\node[draw,minimum height=1.5cm,minimum width=4cm] (A) {IF-CONDITIONAL};
\node[dia] (B1) [below=of A] {Condition 1 (\textit{true?})};
\node[dia] (B2) [below=of B1] {Condition 2 (\textit{true?})};
\node[dia] (B3) [below=of B2] {Condition ... (\textit{true?})};
\node[dia] (B4) [below=of B3] {Else (\textit{true?})};
\draw[ ->] (A) -- (B1);
\draw[ ->] (B1) -- (B2) node[fill=white,midway] {NO};
\draw[ ->] (B2) -- (B3) node[fill=white,midway] {NO};
\draw[ ->] (B3) -- (B4) node[fill=white,midway] {NO};
\foreach \x in {1,...,4}{
\node[draw,minimum width=5cm,minimum height=2cm] (C\x) [right=of B\x] {Statement \x};
\draw[ ->] (B\x) -- (C\x) node[fill=white,midway] {YES};
\draw[ ->] (C\x.east) -- ++(1cm,0);
}
\draw[ ->] (B4.south) |- ++(12cm,-1.5cm) node[fill=white,near start] {NO};
\draw[->] (C1) -| ([xshift=12cm,yshift=-3cm]B4.south);
\end{tikzpicture}
\end{center}
\end{document}

Assigning a range of values to a variable in tikz

I would like to assign a range of values to a variable in LaTeX to be used in a loop within a tikzpicture environment.
In the below code I would like to replace these lines
\begin{tikzpicture}
\foreach \x in {1, 3, 5, 7}
\foreach \y in {2, ..., 5}{
with something like
first_range = {1, 3, 5, 7}
second_range = {2, ..., 5}
\begin{tikzpicture}
\foreach \x in first_range
\foreach \y in second_range{
A complete runnable code section is below:
\documentclass{article}
\usepackage{tikz}
\usepackage{ifthen}
\begin{document}
\begin{tikzpicture}
\foreach \x in {1, 3, 5, 7}
\foreach \y in {2, ..., 5}{
\ifthenelse{\(\x=1 \OR \x=7\) \AND \y = 3}{
%\filldraw[fill=white] (\x, \y) circle (0.2);
\node[] at (\x, \y) {\vdots};
}{
\filldraw[fill=red] (\x, \y) circle (0.2);
}
}
\end{tikzpicture}
\end{document}
You can store the list in a macro:
\documentclass{article}
\usepackage{tikz}
\usepackage{ifthen}
\def\first{1, 3, 5, 7}
\def\second{2, ..., 5}
\begin{document}
\begin{tikzpicture}
\foreach \x in \first
\foreach \y in \second {
\ifthenelse{\(\x=1 \OR \x=7\) \AND \y = 3}{
%\filldraw[fill=white] (\x, \y) circle (0.2);
\node[] at (\x, \y) {\vdots};
}{
\filldraw[fill=red] (\x, \y) circle (0.2);
}
}
\end{tikzpicture}
\end{document}

How can i input boolean tensors to tf.cond() not just one boolean?

Here is what I want to implement f(x) with tensorflow
input x = (x1,x2,x3,x4,x5,x6,x7,x8,x9)
define f(x) = f1(x1,x2,x3,x4,x5) + f2(x5,x6,x7,x8,x9)
where
f1(x1,x2,x3,x4,x5) = {1 if
(x1,x2,x3,x4,x5)=(0,0,0,0,0),
g1(x1,x2,x3,x4,x5) otherwise}
f2(x5,x6,x7,x8,x9) = {1 if
(x5,x6,x7,x8,x9)=(0,0,0,0,0),
g2(x5,x6,x7,x8,x9) otherwise}
This is my tensorflow code
import tensorflow as tf
import numpy as np
ph = tf.placeholder(dtype=tf.float32, shape=[None, 9])
x1 = tf.slice(ph, [0, 0], [-1, 5])
x2 = tf.slice(ph, [0, 4], [-1, 5])
fixed1 = tf.placeholder(dtype=tf.float32, shape=[1, 5])
fixed2 = tf.placeholder(dtype=tf.float32, shape=[1, 5])
# MLP 1
w1 = tf.Variable(tf.ones([5, 1]))
g1 = tf.matmul(x1, w1)
# MLP 2
w2 = tf.Variable(-tf.ones([5, 1]))
g2 = tf.matmul(x2, w2)
check1 = tf.reduce_all(tf.equal(x1, fixed1), axis=1, keep_dims=True)
check2 = tf.reduce_all(tf.equal(x2, fixed2), axis=1, keep_dims=True)
#### with Problem
f1 = tf.cond(check1,
lambda: tf.constant([2], dtype=tf.float32), lambda: g1)
f2 = tf.cond(check2,
lambda: tf.constant([1], dtype=tf.float32), lambda: g2)
####
f = tf.add(f1, f2)
x = np.array([[0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 1],
[1, 0, 0, 0, 0, 0, 0, 0, 0],
[2, 0, 0, 0, 0, 0, 0, 0, 0],
[9, 0, 0, 0, 0, 0, 0, 0, 0]])
fixed = np.array([[0, 0, 0, 0, 0]])
with tf.Session() as sess:
sess.run(tf.global_variables_initializer())
print('(1)\n', sess.run(check1, feed_dict={ph: x, fixed1: fixed, fixed2: fixed}))
print('(2)\n', sess.run(check2, feed_dict={ph: x, fixed1: fixed, fixed2: fixed}))
print('(3)\n', sess.run(f, feed_dict={ph: x, fixed1: fixed, fixed2: fixed}))
print('(4)\n', sess.run(f1, feed_dict={ph: x, fixed1: fixed, fixed2: fixed}))
print('(5)\n', sess.run(f2, feed_dict={ph: x, fixed1: fixed, fixed2: fixed}))
In this case,
check1 is [[ True], [ True], [False], [False], [False]] with shape (5, 1)
check2 is [[ True], [False], [ True], [ True], [ True]] with shape (5, 1)
I expect result of f is [[3], [1], [2], [3], [10]]
but seems like tf.cond() can not handle input as boolean tensors with shape (5, 1)
Could you advice how to implement f(x) with tensorflow, please.
This is Error message what i received
Traceback (most recent call last): File
"C:\Users\hong\AppData\Local\Continuum\Anaconda3\lib\site-packages\tensorflow\python\framework\common_shapes.py",
line 670, in _call_cpp_shape_fn_impl
status) File "C:\Users\hong\AppData\Local\Continuum\Anaconda3\lib\contextlib.py",
line 66, in exit
next(self.gen) File "C:\Users\hong\AppData\Local\Continuum\Anaconda3\lib\site-packages\tensorflow\python\framework\errors_impl.py",
line 469, in raise_exception_on_not_ok_status
pywrap_tensorflow.TF_GetCode(status)) tensorflow.python.framework.errors_impl.InvalidArgumentError: Shape
must be rank 0 but is rank 2 for 'cond/Switch' (op: 'Switch') with
input shapes: [?,1], [?,1].
During handling of the above exception, another exception occurred:
Traceback (most recent call last): File
"C:/Users/hong/Dropbox/MLILAB/Research/GM-MLP/code/tensorflow_cond.py",
line 23, in
lambda: tf.constant([2], dtype=tf.float32), lambda: g1) File "C:\Users\hong\AppData\Local\Continuum\Anaconda3\lib\site-packages\tensorflow\python\ops\control_flow_ops.py",
line 1765, in cond
p_2, p_1 = switch(pred, pred) File "C:\Users\hong\AppData\Local\Continuum\Anaconda3\lib\site-packages\tensorflow\python\ops\control_flow_ops.py",
line 318, in switch
return gen_control_flow_ops._switch(data, pred, name=name) File "C:\Users\hong\AppData\Local\Continuum\Anaconda3\lib\site-packages\tensorflow\python\ops\gen_control_flow_ops.py",
line 368, in _switch
result = _op_def_lib.apply_op("Switch", data=data, pred=pred, name=name) File
"C:\Users\hong\AppData\Local\Continuum\Anaconda3\lib\site-packages\tensorflow\python\framework\op_def_library.py",
line 759, in apply_op
op_def=op_def) File "C:\Users\hong\AppData\Local\Continuum\Anaconda3\lib\site-packages\tensorflow\python\framework\ops.py",
line 2242, in create_op
set_shapes_for_outputs(ret) File "C:\Users\hong\AppData\Local\Continuum\Anaconda3\lib\site-packages\tensorflow\python\framework\ops.py",
line 1617, in set_shapes_for_outputs
shapes = shape_func(op) File "C:\Users\hong\AppData\Local\Continuum\Anaconda3\lib\site-packages\tensorflow\python\framework\ops.py",
line 1568, in call_with_requiring
return call_cpp_shape_fn(op, require_shape_fn=True) File "C:\Users\hong\AppData\Local\Continuum\Anaconda3\lib\site-packages\tensorflow\python\framework\common_shapes.py",
line 610, in call_cpp_shape_fn
debug_python_shape_fn, require_shape_fn) File "C:\Users\hong\AppData\Local\Continuum\Anaconda3\lib\site-packages\tensorflow\python\framework\common_shapes.py",
line 675, in _call_cpp_shape_fn_impl
raise ValueError(err.message) ValueError: Shape must be rank 0 but is rank 2 for 'cond/Switch' (op: 'Switch') with input shapes: [?,1],
[?,1].
Process finished with exit code 1
I think you need tf.where, not tf.cond.
See the answer to this question: How to use tf.cond for batch processing

How can I getting value of 8 neighbor of a image as the third dimension in Numpy

Given an 2d image data, for every pixel P1, how can I get the following 3d array out of it?
P9 P2 P3
P8 P1 P4
P7 P6 P5
img[x,y,:] = [P2, P3, P4, P5, P6, P7, P8, P9, P2]
without using forloop, just numpy operation (because of performance issue)
Here's one approach with zeros padding for boundary elements and using NumPy strides with the built-in scikit-image's view_as_windows for efficient sliding window extraction -
from skimage.util import view_as_windows as viewW
def patches(a, patch_shape):
side_size = patch_shape
ext_size = (side_size[0]-1)//2, (side_size[1]-1)//2
img = np.pad(a, ([ext_size[0]],[ext_size[1]]), 'constant', constant_values=(0))
return viewW(img, patch_shape)
Sample run -
In [98]: a = np.random.randint(0,255,(5,6))
In [99]: a
Out[99]:
array([[139, 176, 141, 172, 192, 81],
[163, 115, 7, 234, 72, 156],
[ 75, 60, 9, 81, 132, 12],
[106, 202, 158, 199, 128, 238],
[161, 33, 211, 233, 151, 52]])
In [100]: out = patches(a, [3,3]) # window size = [3,3]
In [101]: out.shape
Out[101]: (5, 6, 3, 3)
In [102]: out[0,0]
Out[102]:
array([[ 0, 0, 0],
[ 0, 139, 176],
[ 0, 163, 115]])
In [103]: out[0,1]
Out[103]:
array([[ 0, 0, 0],
[139, 176, 141],
[163, 115, 7]])
In [104]: out[-1,-1]
Out[104]:
array([[128, 238, 0],
[151, 52, 0],
[ 0, 0, 0]])
If you want a 3D array, you could add a reshape at the end, like so -
out.reshape(a.shape + (9,))
But, be mindful that this would create a copy instead of the efficient strided based views we would get from the function itself.

Resources