Skip to content

Commit

Permalink
训练::training tang4 tang5 tang7 with one model
Browse files Browse the repository at this point in the history
  • Loading branch information
oovm committed Sep 5, 2018
1 parent 4361531 commit e1e1969
Show file tree
Hide file tree
Showing 5 changed files with 158 additions and 13 deletions.
6 changes: 4 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,15 @@ Thumbs.db

# 模型文件
*.7z
*.mx
*.rar
*.zip
*.wxf
*.wlnet
*.onnx
Training/*/*.wmlf

# 可交换格式
*.json
*.txt
*.csv
*.csv
*.onnx
3 changes: 0 additions & 3 deletions Models/WaifuSY-Tang-7.WMLF

This file was deleted.

156 changes: 151 additions & 5 deletions Training/Tang-RNN/Tang.m
Original file line number Diff line number Diff line change
Expand Up @@ -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] := (
Expand Down Expand Up @@ -76,8 +221,9 @@

(* ::Subsection::Closed:: *)
(*附加设置*)
End[];
SetAttributes[
{ },
{Protected, ReadProtected}
]
End[]
];
EndPackage[]
4 changes: 2 additions & 2 deletions Training/Tang-RNN/Tang.nb
Git LFS file not shown
2 changes: 1 addition & 1 deletion Training/Tang-Tableaux/Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
#### 函数:

> UtahimeTableaux[long,str]
>> long 表示所有诗词的长度\
>> long 表示所有诗词的长度
>> str 表示需要的藏头诗词

Expand Down

0 comments on commit e1e1969

Please sign in to comment.