diff --git a/.gitignore b/.gitignore index 8192fd2..468d14c 100644 --- a/.gitignore +++ b/.gitignore @@ -13,13 +13,15 @@ Thumbs.db # 模型文件 *.7z +*.mx *.rar *.zip *.wxf *.wlnet -*.onnx +Training/*/*.wmlf # 可交换格式 *.json *.txt -*.csv \ No newline at end of file +*.csv +*.onnx \ No newline at end of file diff --git a/Models/WaifuSY-Tang-7.WMLF b/Models/WaifuSY-Tang-7.WMLF deleted file mode 100644 index e0afb45..0000000 --- a/Models/WaifuSY-Tang-7.WMLF +++ /dev/null @@ -1,3 +0,0 @@ -version https://git-lfs.github.com/spec/v1 -oid sha256:c1211bfe966b5f918afb6759ba6f3b57c71592b93e45c02f9fa4ee5b5f2b31e0 -size 13933156 diff --git a/Training/Tang-RNN/Tang.m b/Training/Tang-RNN/Tang.m index f51e76f..f28e27a 100644 --- a/Training/Tang-RNN/Tang.m +++ b/Training/Tang-RNN/Tang.m @@ -20,25 +20,170 @@ (**) (* ::Text:: *) (*这里应该填这个函数的介绍*) +BeginPackage["Tang`"]; (* ::Section:: *) (*函数说明*) Tang7::usage = ""; Tang7Line::usage = ""; Tang7LineRandom::usage = ""; +PrepareDataTang::usage = "PrepareData[char_, f_ : 3]"; +jsonNetPlot::usage = ""; (* ::Section:: *) (*程序包正体*) (* ::Subsection::Closed:: *) (*主设置*) -ExNumber::usage = "程序包的说明,这里抄一遍"; -Begin["`Tang`"]; +Begin["`RNN`"]; (* ::Subsection::Closed:: *) (*主体代码*) Version$Tang = "V1.0"; Updated$Tang = "2018-08-28"; (* ::Subsubsection:: *) (*功能块 1*) + +PrepareDataTang[char_, f_ : 3] := Block[ + {s, sp, ipt, ips, rare, chars}, + s = ToString[char]; + sp = Print@Export[#1, #2, PerformanceGoal -> "Size"]&; + ipt = Select[ + Flatten@Import["Data-Tang.csv"], + StringLength[#] == (char + 1) * 2& + ] // Core$Galaster`TT; + sp["count" <> s <> ".WXF", count = Tally[Characters@StringJoin[ipt]]]; + Echo[Length@ipt, Text["语料数: "]]; + sp["ipt" <> s <> ".WXF", ipt]; + rare = First /@ Select[SortBy[count, Last], Last@# < f&]; + ips = Select[ipt, !StringContainsQ[#, rare]&] // Core$Galaster`TT; + Echo[Length[chars = DeleteDuplicates@Flatten[Characters /@ ips]], "符号数: "]; + sp["chars" <> s <> ".WXF", chars ]; + Now +]; + +inspectPredictions[string_] := Block[ + {obj, chars, pred, predItems, charItems}, + obj = NetStateObject[generateNet]; + chars = Characters[string]; + pred = Map[obj[#, {"TopProbabilities", 9}]&, chars]; + predItems = Map[Item[First[#], Background -> Opacity[Last[#], Darker[Green]]]&, pred, {2}]; + predItems = Prepend[Most[predItems], Table[Item["", Background -> Gray], 9]]; + charItems = Item[#, Background -> LightBlue]& /@ chars; + Grid[Prepend[Transpose[predItems], charItems], Spacings -> {0.6, 0.2}, Dividers -> All, FrameStyle -> Gray] +] + +Options[jsonNetPlot] = { + "ShowTensors" -> True, + "VertexLabels" -> Placed["ID", Above], + "VertexOrder" -> Automatic, + "EdgeBundling" -> False, + "OutputTensors" -> None, + "InternalDimensions" -> None, + Rotate -> False, + GraphLayout -> "LayeredDigraphDrawing" +}; +jsonNetPlot[file_File, OptionsPattern[]] := Block[ + { + showTensors, vertexLabels, vertexOrder, edgeBundling, outputTensors, plot, + internalDimensions, nodes, argnodes, heads, MXNetLink`Visualization`PackagePrivate`$oldids, nameStrings, typeStrings, + edges, nodeOps, longRange, opTypes, opNames, nullType, blank, maxIndex, + name, nodeDims, edgeTooltips, nops, opStyles, opSizes, vertexTypeData, labels, infoGrids, nnodes + }, + { + showTensors, vertexLabels, vertexOrder, edgeBundling, outputTensors, internalDimensions + } = OptionValue @ { + "ShowTensors", "VertexLabels", "VertexOrder", "EdgeBundling", "OutputTensors", "InternalDimensions" + }; + expr = MXNetLink`MXSymbolToJSON@MXNetLink`MXSymbolFromJSON@file; + {nodes, argnodes, heads} = Lookup[expr, {"nodes", "arg_nodes", "heads"}, GeneralUtilities`Panic[]]; + MXNetLink`Visualization`PackagePrivate`$oldids = If[ListQ[vertexOrder], + Map[Function[GeneralUtilities`IndexOf[vertexOrder, #name] - 1], nodes], + Range[Length[nodes]] - 1 + ]; + nodes = Map[Append[#, "inputs1" -> (Part[#inputs, All, 1] + 1)]&, nodes]; + nameStrings = Map[MXNetLink`Visualization`PackagePrivate`toVertexLabelString[#name]&, nodes]; + typeStrings = Map[MXNetLink`Visualization`PackagePrivate`toVertexTypeString[#op]&, nodes]; + AddTo[argnodes, 1]; + AddTo[heads, 1]; + edges = Apply[Join, + MapIndexed[ + If[ + SameQ[#op, "null"], + Nothing, + If[showTensors, + Thread @ Prepend[#2, #inputs1], + Thread @ Prepend[#2, Complement[#inputs1, argnodes]] + ] + ]&, + nodes + ] + ]; + edges = DeleteDuplicates @ edges; + nodeOps = nodes[[All, "op"]]; + If[And[edgeBundling, !FreeQ[nodeOps, "Concat" | "SliceChannel"]], + longRange = MXNetLink`Visualization`PackagePrivate`pickLongRangeEdges[edges, nodes], + longRange = None; + ]; + {opTypes, opNames} = GeneralUtilities`Labelling @ nodeOps; + nullType = GeneralUtilities`IndexOf[opNames, "null"]; + nodes = MapIndexed[Function[Append[#, "id" -> (First[#2] - 1)]], + nodes + ]; + If[showTensors && ListQ[outputTensors], + opTypes = Join[opTypes, ConstantArray[nullType, Length @ outputTensors]]; + argnodes = Join[argnodes, Range[Length[outputTensors]] + Max[edges]]; + nameStrings = Join[nameStrings, outputTensors]; + blank = ConstantArray["", Length @ outputTensors]; + MXNetLink`Visualization`PackagePrivate`$oldids = Join[MXNetLink`Visualization`PackagePrivate`$oldids, blank]; + typeStrings = Join[typeStrings, blank]; + nodes = Join[nodes, blank]; + maxIndex = Max @ edges; + MapIndexed[AppendTo[edges, {First @ #, First[#2] + maxIndex}]&, heads] + ]; + edgeTooltips = If[ + SameQ[internalDimensions, None], + None, + nodeDims = Table[ + name = Internal`UnsafeQuietCheck[nodes[[i, "name"]], None]; + MXNetLink`Visualization`PackagePrivate`toDimLabel @ Lookup[ + internalDimensions, name, If[StringQ[name], + Lookup[internalDimensions, StringJoin[name, "_output"], None], + None + ] + ], + {i, Length @ nodes} + ]; + ((Part[nodeDims, #]&) @@@ edges) /. BatchSize -> "b" + ]; + nops = Length @ opNames; + opStyles = Map[MXNetLink`Visualization`PackagePrivate`opColor, opNames]; + opSizes = ReplacePart[ConstantArray[6, nops], nullType -> 4]; + opStyles = ReplacePart[opStyles, nullType -> Gray]; + opNames = opNames /. "null" -> "Tensor"; + vertexTypeData = <|"VertexStyles" -> opStyles|>; + If[showTensors, + vertexTypeData = Join[vertexTypeData, <|"VertexSizes" -> opSizes|>] + ]; + labels = ReplaceAll[vertexLabels, + {"Name" :> nameStrings, "ID" :> MXNetLink`Visualization`PackagePrivate`$oldids, "Type" :> typeStrings} + ]; + infoGrids = Map[MXNetLink`Visualization`PackagePrivate`nodeInfoGrid, nodes]; + nnodes = Length @ nodes; + plot = GeneralUtilities`LayerPlot[edges, + "VertexLabels" -> labels, "HiddenVertices" -> If[showTensors, None, argnodes], + "VertexTypeData" -> vertexTypeData, "VertexTypeLabels" -> opNames, "MaximumImageSize" -> None, + "VertexSizes" -> 4, "EdgeTooltips" -> edgeTooltips, + "BaseLabelStyle" -> {FontSize -> 7}, "LayoutMethod" -> OptionValue[GraphLayout], + "DuplicateInputVertices" -> True, If[showTensors, "VertexTypes" -> opTypes, "VertexStyles" -> opTypes], + "LongRangeEdges" -> longRange, "Rotated" -> OptionValue[Rotate], "ArrowShape" -> "Chevron", "LegendLabelStyle" -> 8 + ]; + Magnify[plot, 1.5] +] + + + + + + + $TangShiComplex = 10; -WaifuTang7 = Import["WaifuSY-Tang-7.WMLF"] Tang7Line[in_List] := TableForm[Text@*Tang7Line /@ in]; Tang7Line[char_String] := ( @@ -76,8 +221,9 @@ (* ::Subsection::Closed:: *) (*附加设置*) +End[]; SetAttributes[ { }, {Protected, ReadProtected} -] -End[] \ No newline at end of file +]; +EndPackage[] \ No newline at end of file diff --git a/Training/Tang-RNN/Tang.nb b/Training/Tang-RNN/Tang.nb index 9bc1384..361588a 100644 --- a/Training/Tang-RNN/Tang.nb +++ b/Training/Tang-RNN/Tang.nb @@ -1,3 +1,3 @@ version https://git-lfs.github.com/spec/v1 -oid sha256:25c6f8e6a1b263176e7eeff611f2fe95f2fea32f8d60e781e1bc6fdab31ab14e -size 856008 +oid sha256:d912ce12b7db923d7813b9e1f48ea9dc245152d8fccff4f9790e88f126be6a3e +size 1427886 diff --git a/Training/Tang-Tableaux/Readme.md b/Training/Tang-Tableaux/Readme.md index 4adb9e2..5c095ca 100644 --- a/Training/Tang-Tableaux/Readme.md +++ b/Training/Tang-Tableaux/Readme.md @@ -15,7 +15,7 @@ #### 函数: > UtahimeTableaux[long,str] ->> long 表示所有诗词的长度\ +>> long 表示所有诗词的长度 >> str 表示需要的藏头诗词