下面的代码添加一些特定位置的张量内指数(感谢@ mrry的答案在这里)。
indices = [[1, 1]] # A list of coordinates to update.
values = [1.0] # A list of values corresponding to the respective
# coordinate in indices.
shape = [3, 3] # The shape of the corresponding dense tensor, same as `c`.
delta = tf.SparseTensor(indices, values, shape)
Run Code Online (Sandbox Code Playgroud)
例如,鉴于此 -
c = tf.constant([[0.0, 0.0, 0.0],
[0.0, 0.0, 0.0],
[0.0, 0.0, 0.0]])
Run Code Online (Sandbox Code Playgroud)
它会在 [1, 1] 处加 1,结果是
[[0.0, 0.0, 0.0],
[0.0, 1.0, 0.0],
[0.0, 0.0, 0.0]])
Run Code Online (Sandbox Code Playgroud)
问题- …
我正在尝试将python-chess游戏导出到pgn文件.该文件建议-
import chess
.
.
chessBoard = chess.Board()
.
.
#Play the game and when over do below
game = chess.pgn.Game.from_board(chessBoard)
with open('output.pgn', 'a') as the_file:
print(game, file=the_file, end="\n\n")
Run Code Online (Sandbox Code Playgroud)
但该chess.pgn.Game.from_board(chessBoard)
行抛出以下错误 -
AttributeError:模块'chess'没有属性'pgn'
pgn
当我输入时,也会出现在intellisense中,chess.
因此编辑器也可以看到有一个pgn
in chess
.这是在Windows 10上的VS2015中运行的python 3.x.
可能是什么原因造成的?
我有一个Dictionary<string, List<MyObject>>
,我需要运行一些资源密集型操作List<MyObject>
.我正在试图弄清楚我是否可以为Dictionary执行资源密集型任务的每个键创建一个线程,以便每个线程更新其键的List.换句话说,多个线程同时更新字典中的不同项目?
请考虑以下简化的伪代码 -
public void MyMethod() {
//The myDict object needs to be shared by all threads.
Dictionary<string, List<MyObject>> myDict = new Dictionary<string, List<MyObject>>();
//GetKeyValue() may return the same key multiple times
foreach(var kv in GetKeyValue()) {
if(myDict.ContainsKey(kv.Key) { myDict[kv.Key].Add(kv.Value); }
else { myDict.Add(kv.Key, kv.Value); }
Task.Factory.StartNew(() => { RunSubsetSum(kv.Key, myDict); });
}
}
//Resource intensive method
public void RunSubsetSum(string key, Dictionary<string, List<MyObject>> myDict) {
//Lock on key so that no two threads run for …
Run Code Online (Sandbox Code Playgroud) 我试图理解 C# async/await 并观察到这种令人困惑的行为,其中异步方法不执行过去的Task.Delay
调用。
考虑以下 -
class Program
{
static void Main(string[] args)
{
Program p = new Program();
p.MakeBreakfast();
}
public async Task MakeBreakfast()
{
await BoilWater();
StartToaster();
PutTeainWater();
PutBreadinToaster();
SpreadButter();
}
public async Task BoilWater() { Console.WriteLine("BoilWater start"); await Task.Delay(30); Console.WriteLine("BoilWater end"); }
public async Task StartToaster() { Console.WriteLine("StartToaster start"); await Task.Delay(1); Console.WriteLine("StartToaster end"); }
public async Task PutBreadinToaster() { Console.WriteLine("PutBreadinToaster start"); await Task.Delay(2000); Console.WriteLine("PutBreadinToaster end"); }
public async Task PutTeainWater() { Console.WriteLine("PutTeainWater start"); await Task.Delay(30); …
Run Code Online (Sandbox Code Playgroud) 这个正则表达式 - (.*).ap(.\d*)$
在在线正则表达式工具中工作正常(例如,在这里工作),但在C#中没有.
意图是对于以.ap<some numbner>
(以后.ap
是一些数字 - .ap1,.ap123等)结尾的字符串,我们想要捕获最后一个左边的.ap
所有内容和最后一个右边的所有内容.ap
.例如abcfile.csv.ap123
,我们想要捕获abcfile.csv
和123
.
(.*).ap(.\d*)$
在在线工具中按预期捕获这两个组,但在C#中,它捕获整个名称(在上面的示例中捕获abcfile.csv.ap123).
C#代码是 -
string renameSuffix = "ap";
Match match = Regex.Match(filename, @"(.*)\." + renameSuffix + @"(\d*)$");
Run Code Online (Sandbox Code Playgroud)
match.Success为true但match.Captures.Count为1且match.Captures [0] .Value包含整个文件名(我在手表中看着这个).
这可能有什么问题?
更多例子 -